HMcompfreak
HMcompfreak

Reputation: 171

Check whether data in cell of datagridview is null

My DataGridView is bound to a database query result via a SqlDataReader, and when I test a cell value for null, I'm getting unexpected results.

My code is something like this:

SqlConnection con = new SqlConnection(
     "Data Source = .;Initial Catalog = SAHS;integrated security = true");
con.Open();
SqlCommand cmd3 = new SqlCommand(
    "select  Status from vw_stdtfeedetail 
     where Std= 6  and Div ='B' and name='bbkk' and mnthname ='June'", con);
SqlDataReader dr = cmd3.ExecuteReader();
BindingSource bs = new BindingSource();
bs.DataSource = dr;
dataGridView3.DataSource = bs;
this.monthFeeTableAdapter.Fill(this.sAHSDataSet4.MonthFee);
if (dataGridView3.CurrentCell.Value == null)
{
    MessageBox.Show("Pending");
}
else
{
    MessageBox.Show("Already Paid");
}

I'm getting output Already Paid even though the value from database is null.

Upvotes: 1

Views: 1748

Answers (1)

GarethD
GarethD

Reputation: 69749

Try using DBNull.Value instead of null

if (dataGridView3.CurrentCell.Value == DBNull.Value)
{
    MessageBox.Show("Pending");
}
else
{
    MessageBox.Show("Already Paid");
}

Upvotes: 4

Related Questions