Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I stored some images in access db. But i am not able to retrieve and display in picture box. My code is like below. Everything is good but. Breaking at data set.
Please update me where i am doing wrong.

private void LoadPicture()
{
con.Open();
cmd.CommandText = "select picture from pictures where id ='" + listBox1.Text.ToString() + "'";
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
OleDbCommandBuilder cbd = new OleDbCommandBuilder(da);
DataSet ds = new DataSet();


da.Fill(ds); //(getting Error Here)


con.Close();
byte[] ap = (byte[])(ds.Tables[0].Rows[0]["picture"]);
MemoryStream ms = new MemoryStream(ap);
pictureBox1.Image = Image.FromStream(ms);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
label1.Text = listBox1.Text.ToString();
ms.Close();
}
//Updated List Box like below.

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox l = sender as ListBox;
if(l.SelectedIndex != -1)
{
listBox1.SelectedIndex = l.SelectedIndex;
listBox2.SelectedIndex = l.SelectedIndex;
LoadPicture();
}
}
Posted

1 solution

This statement is incorrect:
C#
cmd.CommandText = "select picture from pictures where id ='" +
                  listBox1.Text.ToString() +
                  "'";
It should read:
C#
cmd.CommandText = "select picture from pictures where id ='" +
                  listBox1.SelectedItem.Text +
                  "'";
Of course this assumes an item is selected in listBox1.  Also, your code is guilty of SQL injection, but that's a whole different story.

/ravi
 
Share this answer
 
v2
Comments
satya.Sw 1-Jan-15 19:27pm    
Hi Boss.

Even I changed it. its not working. Please suggest me to get in other way.

My Requirement is to retrieve image from db to picturebox, using listbox.
Ravi Bhavnani 1-Jan-15 20:32pm    
"its not working"
That doesn't tell me anything.

What happens when you debug the app? Is the query correct? Is the item not found in the database? Is there an exception thrown?

/ravi
satya.Sw 6-Jan-15 19:46pm    
While Executing this Code I am Getting Error Like Below.

""An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll""

""Additional information: Data type mismatch in criteria expression.""

How to overcome this Problem. Please Help me.

private void LoadPicture()
{
con.Open();
cmd.CommandText = "select picture from pictures where id ='" + listBox1.SelectedItem.ToString() + "'";
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
OleDbCommandBuilder cbd = new OleDbCommandBuilder(da);
DataSet dst = new DataSet();
da.Fill(dst);
con.Close();
byte[] ap = (byte[])(dst.Tables[0].Rows[0]["picture"]);
MemoryStream ms = new MemoryStream(ap);
pictureBox1.Image = Image.FromStream(ms);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
label1.Text = listBox1.Text.ToString();
ms.Close();
}
Ravi Bhavnani 6-Jan-15 19:51pm    
I'm not surprised. Your query is wrong. Look at my answer again.

/ravi
satya.Sw 6-Jan-15 20:02pm    
Hi Boss,

cmd.CommandText = "select picture from pictures where id ='" +
listBox1.SelectedItem.Text +
"'";

After SelectedItem Keyword text is not accepting.
selectedItem.Tostring() is only there.

Even I tried. But its not allowing. It is sysntax Error.

Sorry boss. I am dotnet learner. If i am missing anything update me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900