i want to retrieve image in picture box from database on datagridview cell click event .
but exception raising like "parameter is not valid"
code below here
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
Image image;
int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["ID"].Value);
SqlConnection con = new SqlConnection(@"Data Source=ITOFASL254\SQLEXPRESS; Initial Catalog=TestDB; Integrated Security=SSPI; user id=sa; password=passpass; Trusted_Connection=False; database=TestDB;");
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("select Image from ImageStore where ID=" + dataGridView1.Rows[e.RowIndex].Cells["ID"].Value, con);
DataSet ds = new DataSet();
adapter.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
byte[] imageData = (byte[])ds.Tables[0].Rows[0][0];
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);
image = Image.FromStream(ms);
}
pictureBox1.Image = image;
}
con.Close();
}
Reply soon
}
Reply soon
VulpesPosted Jul 30, 2012, 9:55 AM
What type of image is it?
Faisal AnsariPosted Jul 29, 2012, 10:35 PM
fahad sheikhjiPosted Jul 28, 2012, 10:24 AM
datagrid cell click event
try this
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
byte[] imageData = (byte[])dataGridView1.Rows[e.RowIndex].Cells["cdCustomerPhoto"].Value;
Image newImage;
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);
newImage = Image.FromStream(ms, true);
}
pictureBox1.Image = newImage;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
it will help you..
Sudhakar ChaudharyPosted Jul 27, 2012, 4:49 AM
where ID=" + dataGridView1.Rows[e.RowIndex].Cells["ID"].Value, con); i think problem is in colored area.
solution:
ID='"+dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString() +"',con
my code....
try
{
SqlCommand command = new SqlCommand("select N_IMG from SCH2 where F_RECNO='" + FRECNO + "'", con);
SqlDataAdapter dp = new SqlDataAdapter(command);
DataSet ds = new DataSet();
byte[] MyData = new byte[0];
dp.Fill(ds, "s");
DataRow myRow;
myRow = ds.Tables["s"].Rows[0];
MyData = (byte[])myRow["N_IMG"];
MemoryStream stream = new MemoryStream(MyData);
pictureBox1.Image = Image.FromStream(stream);
}
catch (Exception ms)
{
MessageBox.Show("There Are No Image of This Person.", "Message Box");
}
VulpesPosted Jul 27, 2012, 4:44 AM