Introduction
Namespace used in this article
ImageUpload Database table structure
- default.aspx - to save the image into the database
- ShowImage.aspx - to show the records including the image into the gridview
- ShowImage.ashx (Generic Hanlder file) - to retrive the image from the database and give it to ShowImage.aspx as Binary data to its html img tag
Saving image to the database in asp.net
Select file to save into the database: <asp:FileUpload runat="server" ID="FileUpload1" /><asp:Button runat="server" ID="btnSave" OnClick="SaveToTheDatabase" Text="Save to the database" /><p><asp:Label ID="lblMessage" runat="server" EnableViewState="false" /></p>protected void SaveToTheDatabase(object sender, EventArgs e){string fileName = FileUpload1.PostedFile.FileName;int fileLength = FileUpload1.PostedFile.ContentLength;byte[] imageBytes = new byte[fileLength];FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, fileLength);string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();using (SqlConnection conn = new SqlConnection(connStr)){string sql = "INSERT INTO ImageUpload (PictureName, PictureFile) VALUES (@pictureName, @pictureFile)";SqlParameter[] prms = new SqlParameter[2];prms[0] = new SqlParameter("@pictureName", SqlDbType.VarChar, 50);prms[0].Value = fileName;prms[1] = new SqlParameter("@pictureFile", SqlDbType.Image);prms[1].Value = imageBytes;using (SqlCommand cmd = new SqlCommand(sql, conn)){cmd.Parameters.AddRange(prms);conn.Open();cmd.ExecuteNonQuery();conn.Close();
}lblMessage.Text = "Picture uploaded successsfully !";
}
}Show image from database into GridView
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"><Columns>
<asp:BoundField HeaderText="AutoID" DataField="AutoID" />
<asp:BoundField HeaderText="Picture Name" DataField="PictureName" />
<asp:TemplateField HeaderText="Picture">
<ItemTemplate>
<img src="ShowImage.ashx?autoid=<%# Eval("AutoId").ToString() %>" width="150" height="100" />
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>protected void Page_Load(object sender, EventArgs e){string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();DataTable table = new DataTable();using (SqlConnection conn = new SqlConnection(connStr)){string sql = "SELECT AutoID, PictureName FROM ImageUpload ORDER BY PictureName";using (SqlCommand cmd = new SqlCommand(sql, conn)){using (SqlDataAdapter ad = new SqlDataAdapter(cmd)){conn.Open();ad.Fill(table);conn.Close();
}
}
}GridView1.DataSource = table;GridView1.DataBind();
}public void ProcessRequest(HttpContext context){if (context.Request.QueryString["autoId"] == null) return;string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();string pictureId = context.Request.QueryString["autoId"];using (SqlConnection conn = new SqlConnection(connStr)){using (SqlCommand cmd = new SqlCommand("SELECT PictureFile FROm ImageUpload WHERE AutoID = @autoId", conn)){cmd.Parameters.Add(new SqlParameter("@autoId", pictureId));conn.Open();using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)){reader.Read();context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("PictureFile")]);reader.Close();
}
}
}
}public bool IsReusable{get
{return true;}
}In the above code snippet, I am checking for the autoId querystring, if its null I am simply returning.
Conclusion
Download Full Code Here
Source Code: dotnetfunda















