Validate URL

Posted by Venkat | Labels: ,

Validate URL 

<asp:RegularExpressionValidator 
            ID="RegularExpressionValidator1"
            runat="server" 
            ValidationExpression="(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"
            ControlToValidate="TextBox1"
            ErrorMessage="Input valid Internet URL!"
            ></asp:RegularExpressionValidator> 
 
Here i update the Regex this will accept the URL like:
http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html 

Validate US Zip Code

Posted by Venkat | Labels: ,

To validate US Zip Code


<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="\d{5}(-\d{4})?"
ControlToValidate="TextBox1"
ErrorMessage="Input valid U.S. Zip Code!"
></asp:RegularExpressionValidator>

Validate US Phone number

Posted by Venkat | Labels: ,

Validate US Phone number


<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"
ControlToValidate="TextBox1"
ErrorMessage="Input valid U.S. Phone Number!"
></asp:RegularExpressionValidator>

Validate US Social Security Number

Posted by Venkat | Labels: ,

Here we have to discuss how to validate US Social security number


<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" validationexpression="\d{3}-\d{2}-\d{4}"
controltovalidate="TextBox1" errormessage="Input valid U.S. Social Security Number!"></asp:regularexpressionvalidator>

Disable past dates in Calendar control

Posted by Venkat | Labels:

Here we are going to see how to disable the past date in the Calendar contorl




<asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar_DayRender"></asp:Calendar>

protected void Calender_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Today.Date)
{
e.Day.IsSelectable = false;
}
}

Upload and show the images from database

Posted by Venkat | Labels: ,

Hi , Here we are going to see how to upload the image file to database ie : we store the image as a binary format in database.

create the table(name myImages) in sqlserver

with the following columns.

Img_Id --> datatype int (Primary Key with identity)

Image_Content --> datatype image

Image_Type --> datatype varchar(50)

Image_Size --> datatype bigint

Now create a page with name ImageUpload.aspx write the following code....

HtmlCode...


<asp:fileupload id="FileUpload1" runat="server">
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button">
</asp:button></asp:fileupload>



CodeBehind..

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);

SqlConnection myConnection = new SqlConnection(@"integrated security=yes;data source=.\sqlexpress;database=aaa");
SqlCommand storeimage = new SqlCommand("INSERT INTO myImages(Image_Content, Image_Type, Image_Size) values (@image, @imagetype, @imagesize)", myConnection);
storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;
storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value = FileUpload1.PostedFile.ContentType;
storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = FileUpload1.PostedFile.ContentLength;

myConnection.Open();
storeimage.ExecuteNonQuery();
myConnection.Close();
}
}

To display the image....

Create one HttpHandler class with name Handler.ashx write the following code...


<%@ WebHandler Language="C#" Class="Handler" %> >

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public class Handler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
SqlConnection myConnection = new SqlConnection(@"integrated security=yes;data source=.\sqlexpress;database=aaa");
myConnection.Open();
string sql = "Select * from myImages where Img_Id=@ImageId";
SqlCommand cmd = new SqlCommand(sql, myConnection);
cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = context.Request.QueryString["id"];
cmd.Prepare();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.ContentType = dr["Image_Type"].ToString();
context.Response.BinaryWrite((byte[])dr["Image_Content"]);
context.Response.End();
dr.Close();
myConnection.Close();

}

public bool IsReusable
{
get
{
return false;
}
}

}

and create one aspx file with name.... ImageDisplay.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#"Handler.ashx?id="+Eval("Img_Id") %>' Height="100" Width="100" />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Img_Id") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("Image_Type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = FetchAllImagesInfo();
GridView1.DataBind();
}
public DataTable FetchAllImagesInfo()
{
string sql = "Select * from myImages";
SqlDataAdapter da = new SqlDataAdapter(sql, @"integrated security=yes;data source=.\sqlexpress;database=aaa");
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}

Get filename without extension

Posted by Venkat | Labels:

To get the filename without extension

There's an existing method in the System.IO namespace which can return a file name without the extension. You can call this passing in either a full file path, or just a file name.

For example:


string fileName = System.IO.Path.GetFileNameWithoutExtension("building.jpg")

Get details error msg through email

Posted by Venkat | Labels:

Here we discuss how to get the html errormessage ie: yellow asp.net error message ie: show on stack trace if any error occurs in our project so we have to pass this msg to user through email.

we just get that Asp.net yellow error msg

this is the code

Exception err = Server.GetLastError();
Response.Clear();
HttpUnhandledException httpUnhEx = err as HttpUnhandledException;
if (httpUnhEx != null)
{
Response.Write("<h1>ASP.NET Error Page:
\n"+ httpUnhEx.GetHtmlErrorMessage());
}

how to pass if condition through the inline HTML Code

Posted by Venkat | Labels: ,

Here is the code we are passing the if condition through inline code..

ex:

Visible='<%# IIf((((Eval("productsize")).ToString().Length > 0) OrElse (Decimal.Parse(Eval("productsize
")) <= 0)), "true", "false") %>'

instead of using some function or procedure or by writing code on Gridview_Rowdatabound
just we simple pass the condition to make the control visible True or flase

Download a file from database

Posted by Venkat | Labels: ,

Hi , we have to discuss about how to download a file from the database , we generally upload a file to database in binary form so how to download that file that is going to see here..

suppose if i upload a file and bind the file detail in Gridview or datalist there i have give the Download link - here i am passing the id of the specific row so using this is i will retrieve the file.
so we pass the id to other page to make the file as download.



If Not Request.QueryString("id") Is Nothing Then
Dim As New SqlCommand("Select * from where download_id like @id", con)
Dim ID As New SqlParameter("@ID", SqlDbType.SmallInt, 2)
ID.Value = Request.QueryString("id")
ID.Direction = ParameterDirection.Input
cmd.Parameters.Add(ID)
cmd.Connection = con
con.Open()
Dim dRE As SqlDataReader
dRE = cmd.ExecuteReader()
While dRE.Read()
Dim myTitle As String = dRE.GetString(4).ToString 'document title
Dim myType As String = dRE.GetValue(5).ToString 'document type
If myTitle = "None" Or myType = "None" Then
message.Text = "No Attachements Present !"
Exit Sub
Else
Dim myDoc = dRE.GetSqlBinary(3) 'document


Response.Buffer = True
Response.Clear()
Response.AddHeader("content-disposition", "attachment; filename=" & myTitle)
'application/octet-stream
Select Case myType.ToLower
Case "doc"
Response.ContentType = "application/msword"
Case "docx"
Response.ContentType = "application/msword"
Case "ppt"
Response.ContentType = "application/vnd.ms-powerpoint"
Case "xls"
Response.ContentType = "application/x-msexcel"
Case "htm"
Response.ContentType = "text/HTML"
Case "html"
Response.ContentType = "text/HTML"
Case "jpg"
Response.ContentType = "image/JPEG"
Case "gif"
Response.ContentType = "image/GIF"
Case "pdf"
Response.ContentType = "application/pdf"
Case Else
Response.ContentType = "text/plain"
End Select
Response.BinaryWrite(myDoc.Value)
Response.Flush()
Response.Close()
If myDoc.isNull Then
message.Text &= "Retrieving File: " _
& myTitle & "." & myType & " Size: NULL
"
Else
message.Text &= "Retrieving File: " _
& myTitle & "." & myType & " Size: " & myDoc.ToString() & "
"
End If
End If
End While
con.Dispose()
con = Nothing
cmdDownloadDoc.Dispose()
cmdDownloadDoc = Nothing

End If
End Sub

PayOffers.in