I want to insert the image into database & Retrive the data back & display into DataGridView..
Foll is the code the inserts the image data into Databse--
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == null
|| FileUpload1.PostedFile.FileName == "")
{
lblErrors.Text = "Please Select the file";
}
else
{
byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
query = "Insert into ImageGallery1 values(@Image_Content)";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@Image_Content", myimage);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
I drag the Grdview onto the second form--
SOURCE TAB
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Template">
<itemtemplate>
<asp:Image ID="Image1" runat="server" />
</itemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Foll code raed the binary data from the database,but mine code is displaying image on page load-
public partial class Default2 : System.Web.UI.Page
{
string query;
SqlConnection conn = new SqlConnection("Data Source=SONIA-B408A4159\\SQLEXPRESS;Initial catalog=sonia;Integrated Security=true");
SqlDataReader dr;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
query = "select * from ImageGallery1";
conn.Open();
cmd=new SqlCommand (query ,conn );
dr = cmd.ExecuteReader();
while (dr.Read())
{
int contentlength = Convert .ToInt32 (dr.GetBytes(0, 0, null, 0, int.MaxValue));
byte[] buffer = new byte[contentlength];
dr.GetBytes(0, 0, buffer, 0, contentlength);
Response.BinaryWrite(buffer);
}
}
}
I do not know how to insert image into Gridview,Can somebody tell me how to do that??//