0

I'm a newbie in C# programming language. I have problem on how can I check either the image is exist or not in database? I'm using database inner join. My current code is work. But if the image not exist in database, all the text box does not get value from database.I don't want like that. I want like this > if image not exist, all the text box got their value from database, even though image not exist. Please somebody help me. Here is my code:

    private void textBoxEmplNo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (textBoxEmplNo.Text != "")
                {

                    string selectSql = "select a.name, a.empno, a.workno, a.icnum, a.passport, a.deptno, a.section, a.designation, b.path from m_employee a inner join m_emp_photo b on b.empno=a.empno where a.empno= @empno";

                    SqlCommand cmd = new SqlCommand(selectSql, con);

                    cmd.Parameters.AddWithValue("@empno", textBoxEmplNo.Text);

                    bool isDataFound = false;

                    try
                    {

                        con.Open();

                        using (SqlDataReader read = cmd.ExecuteReader())
                        {
                            while (read.Read())
                            {
                                isDataFound = true;

                                textBoxWorkNo.Text = (read["workno"].ToString());
                                textBoxName.Text = (read["name"].ToString());
                                textBoxICPass.Text = (read["icnum"].ToString());
                                textBoxPassport.Text = (read["passport"].ToString());
                                textBoxDept.Text = (read["deptno"].ToString());
                                textBoxSection.Text = (read["section"].ToString());
                                textBoxDesignation.Text = (read["designation"].ToString());

                                pictureBox1.Visible = true;
                                pictureBox1.Image = Image.FromFile("" + read["path"].ToString());

                                dataGridView1.Visible = false;
                            }
                        }

                        if (!isDataFound)
                        {
                            textBoxEmplNo.Text = "";
                            textBoxWorkNo.Text = "";
                            textBoxName.Text = "";

                            // Display message here that no values found
                            MessageBox.Show("No Result Found");

                        }
                    }
                    finally
                    {


                        con.Close();
                    }

                }

                else
                {
                    textBoxWorkNo.Text = "";
                    textBoxName.Text = "";
                }

                    dataGridView1.Visible = false;

            }
        }
Miza
  • 49
  • 1
  • 1
  • 8
  • 1
    You want to show employee data from `m_employee ` irrespective of data present in photo table right ? For that just use the `left join`. – Mahesh Jun 13 '17 at 02:24
  • @CoderofCode Hi, I already try. But it still show message "No Result Found" if image not have in database. I want the employee data still appears for all text box, even though the image not exist in database. Can u explain more details with code? Because I'm still newbie in programming. :( – Miza Jun 13 '17 at 02:30
  • I have wrote answer for this. Please check it out – Mahesh Jun 13 '17 at 02:31
  • @CoderofCode I already try it but it appears error like this > _The path is not of a legal form._ why? – Miza Jun 13 '17 at 02:39
  • in my answer I explained this, that is because of `NULL` value from database you need to check it before setting it to path. [Here is example](https://stackoverflow.com/questions/10431835/dbnull-if-statement) to how to do that. – Mahesh Jun 13 '17 at 02:41

1 Answers1

0

From my understanding you want to show employee data irrespective of his photo present of not in database so do the left join on tables so your query changes as below,

select a.name, a.empno, a.workno, a.icnum, a.passport, a.deptno, a.section, a.designation, b.path 
from 
     m_employee a left join m_emp_photo b 
     on b.empno=a.empno 
where 
     a.empno= @empno

This will give you all records for given employee number from left side table and matching from right side if no values available then NULL for that row from right side table.

Although you need to handle the NULL value for b.Path in the code.

so in code before this code line,

pictureBox1.Image = Image.FromFile("" + read["path"].ToString());

check if path is DBNULL or not by comparing with System.DBNull.Value.

Mahesh
  • 8,694
  • 2
  • 32
  • 53