Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Wednesday, January 27, 2016

how to check Null (isDBNull) in C# Data table and Empty string.


how to check Null (isDBNull) in C# Datatable

IsNull method of the DataRow is use to check null value.

Example



     foreach (DataRow row in table.Rows)
       {
           if (row.IsNull("id"))
           {
               Response.Write("success");
           }
       }

Similer IsNullOrEmpty method is use to check string value null or empty.

Example



  public static void Main() 
    {
    string s1 = "abcd";
    string s2 = "";
    string s3 = null;

    Console.WriteLine("String s1 {0}.", Test(s1));
    Console.WriteLine("String s2 {0}.", Test(s2));
    Console.WriteLine("String s3 {0}.", Test(s3));
    }

    public static String Test(string s)
    {
    if (String.IsNullOrEmpty(s)) 
        return "is null or empty";
    else
        return String.Format("(\"{0}\") is neither null nor empty", s);
    }

0 comments:

Post a Comment