hi everyone,
i have been trying to display an image selected from my sql database in a picturebox.below is the code im using
SqlConnection cn = new SqlConnection("Data Source=KATOTO-PC;Initial Catalog =AssetRegister;User ID=sa;Password=Kyozi");
string sql = ("select serialnumber,supplier,assetimage from AssetInformation where serialnumber like '" + textBox1.Text + "'");
SqlCommand cmd = new SqlCommand(sql, cn);
cn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read(); // read first row
textBox2.Text = sdr[2].ToString(); // read first column
if (sdr.HasRows)
{
while (sdr.Read())
{
byte[] getimage = (byte[])sdr[2];
MemoryStream ms = new MemoryStream(getimage);
Image myimage = Image.FromStream(ms);
pictureBox2.Image = myimage;
}
sdr.Close();
}
however nothing is displayed in my picturebox,but if i change line "textBox2.Text = sdr[2].ToString(); // read first column" to sdr[2] which is the column of my image, it brings "System.Byte[]"
any help.
best regards.
Loading

SenthilkumarPosted Mar 12, 2012, 12:54 AM
byte[] getimage = (byte[])sdr[2];
MemoryStream ms = new MemoryStream(getimage);
pictureBox2.Image = New System.Drawing.Bitmap(ms);
Hope this will help you to solve. If it useful then mark it as accept answer.
Marvin kakuruPosted Mar 12, 2012, 2:33 AM
Marvin kakuruPosted Mar 12, 2012, 2:31 AM
thanks i have used the code you recomended, however there is still image appearing in my picturebox. could it be some settings on my picturebox or some code that i am missing.
Marvin kakuruPosted Mar 11, 2012, 1:32 PM
maybe you can give me some guidance.
thanks man and best regards.
SenthilkumarPosted Mar 10, 2012, 9:56 PM
The first column should be read as sdr[0]
select serialnumber,supplier,assetimage from AssetInformation
The reader will read the rows and the columns has to be read as follows.
first column = sdr[0].ToString();
second column = sdr[1].ToString();
third column = sdr[2].ToString();
Its typically starts from 0 to 2.
Hope this will help you to solve this issue.
If it is useful then mark it as accepted answer.