How to check image is exist or not in database and show in picturebox using combobox value in c# windows application

  • Thread starter Thread starter John6272
  • Start date Start date
J

John6272

Guest
hi, i am using windows form, i write a code for getting a image from database in picture box when combo box value is selected. my code is working correctly when combo box value is select and show the data (only show data that have a image). BUT i have a data without a image, when i select combo box value to show data that have no image, it show me a "ERROR" - "Parameter is not valid".

i tried if condition on it but code don't work for me.

here is the code...

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=combolist.db;Version=3;"))
{
string CommandText = "SELECT * FROM combo WHERE [Id]=@id";
using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
{
cmd.Parameters.AddWithValue("@id", comboBox1.SelectedItem.ToString());
conn.Open();
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox1.Text = dr["Id"].ToString();
textBox2.Text = dr["FirstName"].ToString();
textBox3.Text = dr["LastName"].ToString();
textBox4.Text = dr["Age"].ToString();
textBox5.Text = dr["Address"].ToString();

byte[] img = (byte[])(dr["Pic"]);
if (img == null)
{
pictureBox1.Image = null;
}
else
{
MemoryStream ms = new MemoryStream(img);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Please help me... Thanks

Continue reading...
 
Back
Top