Skip to content

Image

Marvin kakuru

Marvin kakuru

Asked Mar 10, 2012, 11:24 AM ESTSolved
Share
LinkedInX
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.

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    ✓ Accepted answer0
    You try this.

    Code
                       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.

    0
  • Senthilkumar
    0
    Hi,

    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.
    0
  • Marvin kakuru
    0
    Hi Senthilkumar, thanks i have managed to change and get sdr[2] to display the third column, however my real problem is that i can not view the image i am trying to select in the picturebox.
    maybe you can give me some guidance.
    thanks man and best regards.
    0
  • Marvin kakuru
    0
    Senthilkumar,
    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. 
    0
  • Marvin kakuru
    0
    sorry i meant to say there is still no image appearing.pardon my english
    0