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

Guest UserRank #816 · 2.8K reputationPosted Apr 11, 2022, 9:18 AM EDT
Nitin SontakkeRank #264 · 9.6K reputationPosted Apr 11, 2022, 9:50 AM EDT
Valerie MeunierRank #1,526 · 1K reputationPosted Apr 11, 2022, 9:41 AM EDT
Nitin SontakkeRank #264 · 9.6K reputationPosted Apr 11, 2022, 9:51 AM EDT
Paul RajsRank #2,423 · 582 reputationPosted Apr 13, 2022, 5:32 AM EDT