Sunday, October 26, 2014

  • Best way to check if a Data Table has a null (SQL) value in it


    Hostgator Offers


     Check Whole table



    If you want to check if a null value exists in the table you can use this method:
    public static bool HasNull(this DataTable table)
    {
        foreach (DataColumn column in table.Columns)
        {
            if (table.Rows.OfType<DataRow>().Any(r => r.IsNull(column)))
                return true;
        }
    
        return false;
    }
     
     
    and use 
     
    table.HasNull(); 


     where ever you need


     Checking Specific column


    Compare the value of the column to the DBNull.Value value to filter and manage null values in whatever way you see fit.

     
     
     
     
    foreach(DataRow row in table.Rows)
    {
        object value = row["ColumnName"];
        if (value == DBNull.Value)
            // do something
        else
            // do something else
    }
     
     
    Check More About DBNull Class
     
     
     
     
  • 0 comments:

    Post a Comment

    Copyright @ 2013 Code Snippets.