0

How to check selected combobox value is null? I am filling comboboxes with these functions:

string sql = "select employee from db_company";
SqlDataReader dr = Db.loadData(sql);
if(cbox.Items.Count == 0)
{
    while (dr.Read())
    {
        cbox.Items.Add(dr[0].ToString());
    }
}

In another class, loadData function is:

public SqlDataReader loadData(string sql, params object[] parameters)
{
    SqlCommand command = new SqlCommand(sql, Db);
    for (int i = 0; i < parameters.Length;)
    {
        command.Parameters.AddWithValue(parameters[i++] as string, parameters[i++]);
    }
    SqlDataReader dr = command.ExecuteReader();
    return dr;
}  

and I'm trying to check with this function:

public bool nullCheck(params object[] parameters)
{
    bool check = true;
    for(int i = 0; i < parameters.Length; i++)
    {
        if (parameters[i] == null)
            check = false;
    }
    return check;
}

And when I pass combobox or textBox values as parameters to nullCheck(), the debug says for comboBox,

System.Windows.Forms.ComboBox, Items.Count 14

I'm trying to figure out how can I handle this, I tried some ways, for example showing the value of combobox, however messagebox shows null.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Cemil Çakmak
  • 149
  • 1
  • 12
  • 1
    And the debug should say `System.Windows.Forms.TextBox` for `TextBox` controls as well, because you probably passed the controls to `nullCheck` method. While this is not a good validation mechanism, but if for any reason you want to continue it, at least you need to pass `ComboBox.SelectedItem` and `TextBox.Text` to your `nullCheck` method. – Reza Aghaei Dec 27 '19 at 08:23
  • Off-topic but take a look at [naming guidelines](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines?WT.mc_id=DT-MVP-5003235). – Reza Aghaei Dec 27 '19 at 08:29
  • Take a look at this post about [Validation in Windows Forms](https://stackoverflow.com/a/35993185/3110834), see this example about [Validating event and Validation summary](https://stackoverflow.com/a/33080822/3110834). You may also want to know about using [DataAnnotations Validation Attributes in Windows Forms](http://www.reza-aghaei.com/dataannotations-validation-attributes-in-windows-forms/). – Reza Aghaei Dec 27 '19 at 08:38
  • @RezaAghaei thanks for your guidance, I am going to read them – Cemil Çakmak Dec 27 '19 at 08:50

1 Answers1

0

I am not sure please try if (parameters[i].ToLower() == "null")