C# .NET - how to display a image by fileupload using jquery or javascript and path will store on db?

Asked By Giri s on 02-Jul-12 09:44 AM

in web page ,image control and file upload are there.if click file upload and select image file from local disc and it will display that image in asp.net image control without clicking another buttons or refreshing the page and before going to display that image its path has to be stored in database

how to do this using jquery or javascript in asp.net????

Lalit M replied to Giri s on 02-Jul-12 11:41 AM
if you don't want to use javascript then show below code how to display image from db to control.
    <form id="form1" runat="server">
    <asp:ImageButton ID="ImageButton1" ImageUrl="~/Displaypicture.aspx?ImageID=1" runat="server" />

Codebehind of Displaypicture.aspx:

Set the backgound image in Lable to retrieve image,


read image and show in label as

The sample code follows
public void ProcessRequest (HttpContext context)
  SqlConnection myConnection = new SqlConnection(“YourConnectionString”);
  string sql = "Select Image_Content from ImageGallery where Img_Id=@ImageId";
  SqlCommand cmd = new SqlCommand(sql, myConnection);
  cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = context.Request.QueryString["id"];
  SqlDataReader dr = cmd.ExecuteReader();

string str;

str = "<table>";

str = str + "<tr><td>";

str = str + "<img id='img' runat='server' src=" + dr["Image_Content"].ToString() + "/>"';

str = str + "</td></tr>";

str = "</table>";

lblImage.Text = str.ToString();