Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 82.2k

how to check whether a sqlserver field is empty or not?

Apr 11 2022 12:49 PM

Hi,

Field 'city' (char(20)) in a table of sqlserver contains 9 rows: some are filled, some are Null and some don't contain anything (empty).
New York
Paris
Null

Toronto

Null
Null
Brussels

I created a datagridview in windows forms and want to get in variable 'city' either the name of the city or 'no city'.

 private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        { 
     string afb = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            if (String.IsNullOrEmpty(afb)) // when Null or empty
                city = "no city";
            else
                city = afb;
       MessageBox.Show(city);
    }

My problem is this:
when i click in the rowheader (in the datagridview) of a filled city, i get the cityname: that's ok; when clicking in the rowheader of a NULL, i get "no city": this is also ok, but when i click in the rowheader of a empty city, i get nothing. Why? I thought String.IsNullOrEmpty checked Null and empty. How can i fix this?


I also tried with this code but same result: 'city' contains nothing when the field is empty.
 if (afb.Equals(System.DBNull.Value) || afb == "")
        city = "no city";
 else
           city = afb;

I also tried with this: doesn't work either
 if (afb.Length == 0)
            city = "no city";
 else
            city = afb;

Thanks

 


Answers (5)