none
Data is Null. This method or property cannot be called on Null values.

    Question

  • Hi,

    in sql table qty is null and rate is null

    so i get the error "Data is Null. This method or property cannot be called on Null values."

    how can i solve it...any one give me suggestions...pls..


     private void Form1_Load(object sender, EventArgs e)
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "Select ITEM,QTY,RATE from Product";
                dr=cmd.ExecuteReader();
                int rr = 0;
                while (dr.Read())
                {
                    rr = dataGridView1.Rows.Add();
                 dataGridView1.Rows[rr].Cells["Column2"].Value = dr.GetString(0);
                    dataGridView1.Rows[rr].Cells["Column3"].Value = dr.GetInt32(1);
                    dataGridView1.Rows[rr].Cells["Column4"].Value = dr.GetInt32(2);
                }
                con.Close();  
            }




    Mankatha


    Sunday, April 01, 2012 3:41 PM

Answers

  • How do you want to display NULL database values in your grid view?  The example below displays NULL values as zero.

    dataGridView1.Rows[rr].Cells["Column3"].Value = dr.IsDBNull(1) ? 0 : dr.GetInt32(1);


    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    • Marked as answer by Mankatha da Sunday, April 01, 2012 4:49 PM
    Sunday, April 01, 2012 3:50 PM
  • hi in this gridview shown dbnull values are 0 ,instead of 0....i need cells are blank ,,,..

    In that case, you'll need to convert the value to a string, with blank representing a DBNull.Value:

    dataGridView1.Rows[rr].Cells["Column3"].Value = dr.IsDBNull(1) ? "" : dr.GetInt32(1).ToString();


    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    • Marked as answer by Mankatha da Monday, April 02, 2012 1:56 AM
    Sunday, April 01, 2012 6:10 PM

All replies

  • How do you want to display NULL database values in your grid view?  The example below displays NULL values as zero.

    dataGridView1.Rows[rr].Cells["Column3"].Value = dr.IsDBNull(1) ? 0 : dr.GetInt32(1);


    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    • Marked as answer by Mankatha da Sunday, April 01, 2012 4:49 PM
    Sunday, April 01, 2012 3:50 PM
  • thnks :)

    Mankatha

    Sunday, April 01, 2012 4:49 PM
  • hi in this gridview shown dbnull values are 0 ,instead of 0....i need cells are blank ,,,..

    is this possible.....


    Mankatha

    Sunday, April 01, 2012 4:53 PM
  • hi in this gridview shown dbnull values are 0 ,instead of 0....i need cells are blank ,,,..

    In that case, you'll need to convert the value to a string, with blank representing a DBNull.Value:

    dataGridView1.Rows[rr].Cells["Column3"].Value = dr.IsDBNull(1) ? "" : dr.GetInt32(1).ToString();


    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/

    • Marked as answer by Mankatha da Monday, April 02, 2012 1:56 AM
    Sunday, April 01, 2012 6:10 PM