Hi everyone,
I am trying to select data including data that is saved as an image in my sql database, and view the image in a picturebox, below is the code I am using but it is retrieving NO image in the picturebox
{
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 = new System.Drawing.Bitmap(ms);
}
sdr.Close();
}
}
Please any help will greatly appreciated because I had this trouble for quite a while now.
Best regards.

VulpesPosted Mar 24, 2012, 9:48 AM
Marvin kakuruPosted Mar 24, 2012, 2:52 PM
you were right the problem has all along been with the way i was saving the image. thanks alot i can now view the images in my picturebox
Thanks again.
Best Regards.
Marvin kakuruPosted Mar 24, 2012, 3:14 AM
i use the code below to load the image into my picture box
private void label31_Click(object sender, EventArgs e)
{
try
{
FileDialog fldlg = new OpenFileDialog();
fldlg.InitialDirectory = @"C:\Users\katoto\Documents\Visual Studio 2010\Projects\Asset Register New\Asset Images";
fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
if (fldlg.ShowDialog() == DialogResult.OK)
{
imagename = fldlg.FileName;
Bitmap newimg = new Bitmap(imagename);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = (Image)newimg;
}
fldlg = null;
}
catch (System.ArgumentException ae)
{
imagename = " ";
MessageBox.Show(ae.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
label31.Visible = false;
}
the i save the image loaded in the picturebox using the code belowSqlConnection conn = new SqlConnection("Data Source=KATOTO-PC;Initial Catalog =AssetRegister;User ID=sa;Password=Kyozi");
conn.Open();
string insertString = "insert into AssetInformation (uniqueassetnumber,barcodenumber,serialnumber,model,manufacturer,assetdescription,supplier,purchaseordernumber,invoicenumber,AssetImage,companyname,companyid) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" +pictureBox1.Image + "','" + label33.Text + "','" + label40.Text + "')";
SqlCommand cmd = new SqlCommand(insertString, conn);
cmd.ExecuteNonQuery();
i think there could be a problem with the way i am saving the image.
VulpesPosted Mar 23, 2012, 1:06 PM
You could check that the stream actually contains something by inserting the highlighted line:
If it does, then another possibility is that the image type is not one which .NET supports. What type of image is it?
Marvin kakuruPosted Mar 22, 2012, 11:53 PM
My picture is in binary form in mysql could that be the problem?
Your help is greatly appreciated.
Best regards.
VulpesPosted Mar 22, 2012, 3:55 PM
Marvin kakuruPosted Mar 22, 2012, 3:31 PM
thanks for your swift response, i have used the code you prescribed but im getting a "ArgumentException was unhandled"
see where on the code below
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();
if (sdr.HasRows)
{
sdr.Read(); // read first row
textBox2.Text = sdr[0].ToString(); // read first column
byte[] getimage = (byte[])sdr[2]; // read third column
MemoryStream ms = new MemoryStream(getimage);
pictureBox2.Image = Image.FromStream(ms); HERE !! "ERROR" "parameter is not valid"
}
sdr.Close();
is there anything i have not done right?
thanks again
VulpesPosted Mar 22, 2012, 12:58 PM
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();
if (sdr.HasRows)
{
sdr.Read(); // read first row
textBox2.Text = sdr[0].ToString(); // read first column
byte[] getimage = (byte[])sdr[2]; // read third column
MemoryStream ms = new MemoryStream(getimage);
pictureBox2.Image = Image.FromStream(ms);
}
sdr.Close();