Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

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

$
0
0

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


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>