Reputation: 11
I want to insert/delete,update image in sql database using asp.net. My code of inserting is:
Stream strm = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(strm);
byte[] img = br.ReadBytes(Convert.ToInt32(strm.Length));
imgDetail.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(img, 0, img.Length);
imgDetail.Visible = true;
try
{
cmd = new SqlCommand("insert into Persons (id,Name,Surname,Address,Phone,Picture,Department) values (@id,@Name,@Surname,@Address,@Phone,@img,@Department)",con);
cmd.Parameters.AddWithValue("id", txtId.Text);
cmd.Parameters.AddWithValue("Name", txtName.Text);
cmd.Parameters.AddWithValue("Surname", txtSurname.Text);
cmd.Parameters.AddWithValue("Address", txtAddress.Text);
cmd.Parameters.AddWithValue("Phone", txtPhone.Text);
cmd.Parameters.AddWithValue("img", img);
cmd.Parameters.AddWithValue("Department", Department.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
I think something went wrong with added image, maybe i do this wrong .Now when i click on some record in my gridview, all data of the record displays in the textboxes, but i cant do display image in imagebox. how can i do this? this is part of displaing data in textboxes:
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex == GridView1.SelectedIndex)
{
GridViewRow row1 = GridView1.SelectedRow;
txtId.Text = row.Cells[0].Text;
txtName.Text = row.Cells[1].Text;
txtSurname.Text = row.Cells[2].Text;
txtAddress.Text = row.Cells[3].Text;
txtPhone.Text = row.Cells[4].Text;
Department.Text = row.Cells[5].Text;
Image.??????
Upvotes: 0
Views: 1197
Reputation: 4451
The "Image" control is a just a wrapper around the standard <img>
HTML-tag. So obviously it does not have any binary property, that you can set to a byte-array.
But you go with a "data" uri.
Here's an idea for you:
Image.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])imagedata);
Upvotes: 0