0

I am getting error while executing this method here:

public void chercher_Employe(System.Data.DataSet Dset, String critere, out String erreur, out Boolean
        exist, out employe tab_emp)
        {

                exist = false;
                erreur = null;

    ////////// here where the error happens
                if (Dset.Tables["employe"].Rows.Count > 0)
                 {
                    ligne = Dset.Tables[table].Select(critere);
                    if (ligne.Count() > 0)
                    {
                        exist = true;
                        tab_emp = new employe();
                        tab_emp.num_e = Convert.ToInt32(ligne[0]["num_e"]);
                        tab_emp.nom_e = ligne[0]["nom_e"].ToString();
                        tab_emp.num_r = Convert.ToInt32(ligne[0]["num_r"]);
                        tab_emp.sal_e = Convert.ToInt32(ligne[0]["sal_e"]);
                        tab_emp.adr_e = ligne[0]["adr_e"].ToString();
                        tab_emp.nom_ser = ligne[0]["nom_ser"].ToString();
                    }
                    else
                    { exist = false; }
                }
                else erreur = "Table employé est vide";
            }

Thats what i get when i execute this method: enter image description here

Thanks in advance.

jmj
  • 649
  • 8
  • 17

1 Answers1

0

Here are the two lines of code to focus on"

/* #1 */ ligne = Dset.Tables[table].Select(critere);
/* #2 */ if (ligne.Count() > 0)

Line 1 is calling a table result based upon your query. If nothing matches the criteria of the query, no rows will be returned; thus ligne has a value of null. A table == null has not Count() property and throws the exception. The easiest fix is to update line #2 to catch the null:

if ((ligne != null) && (ligne.Count() > 0))
Mad Myche
  • 1,075
  • 1
  • 7
  • 15