myconnection = New SqlConnection("server=(local);Initial Catalog=stock;Integrated Security=SSPI")
myconnection.Open()
mycommand = New SqlCommand("select cartype,eng,color,price,year,extras,picture,carname from info where sno='" + txtsno.Text + "'", myconnection)
dr = mycommand.ExecuteReader
While dr.Read()
txttype.Text = dr("cartype").ToString()
txteng.Text = dr("eng").ToString()
txtcolor.Text = dr("color").ToString()
txtcarname.Text = dr("carname").ToString()
txtprice.Text = dr("price").ToString()
txtyear.Text = dr("year").ToString()
txtextras.Text = dr("extras").ToString()
txtpics.Text = dr("picture").ToString()
End While
dr.Close()
Dim da As New System.Data.SqlClient.SqlDataAdapter(mycommand)
Dim ds As New DataSet()
da.Fill(ds)
Dim bits As Byte() = CType(ds.Tables("info").Rows(0)("picture"), Byte())
Dim memorybits As New IO.MemoryStream(bits)
Dim bitmap As New Bitmap(memorybits)
PictureBox1.Image = bitmap
myconnection.Close()
iwant to display image stored in picture field bt am gettinf error, plse help me to do this
Ashish ShuklaPosted Nov 16, 2010, 5:37 AM
We can not directly assign the byte array to the image control. For retreiving image we need to write the byte array retreived from the datbase to the file which we can assign to the Image control.
Below is the sample code.
SqlConnection con=new SqlConnection();
//set the connection string below
con.ConnectionString = "";
SqlCommand cmdSelect = new SqlCommand("select Picture" +
" from TableName where ID=@ID",con);
cmdSelect.Parameters.Add("@ID",SqlDbType.Int, 4);
//set the id below
cmdSelect.Parameters["@ID"].Value = "2";
con.Open();
byte[] barrImg = (byte[])cmdSelect.ExecuteScalar();
string strfn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs = new FileStream(strfn,
FileMode.CreateNew, FileAccess.Write);
fs.Write(barrImg, 0, barrImg.Length);
fs.Flush();
fs.Close();
Image1.Image =System.Drawing.Image.FromFile(strfn);
vincent mapalalaPosted Nov 16, 2010, 5:15 AM
Manikavelu VelayuthamPosted Nov 16, 2010, 4:29 AM