locked
how to check sqldatareader is null or not null RRS feed

  • Question

  • hi all

      how to check sqldatareader not equal to null ?

     i am using 

    if(reader !=null) // condition is not working.

    {

    ....

    }

    plz guide me.

    Regards

    Selva

    Wednesday, August 22, 2012 12:24 PM

Answers

  • if (reader["YourColumn"] != DBNull.Value)
    {
        somestring.Text = "NotNull";
    }
    else
    {
         somestring.Text = "IsNull";
    }


    //SFP

    • Marked as answer by Lisa Zhu Thursday, August 30, 2012 9:03 AM
    Wednesday, August 22, 2012 12:28 PM
  • Another way....

    if (myReader.Read()) 
    {      
    ltlAdditional.Text = "Contains data"; 
    } 
    else 
    {        
    ltlAdditional.Text = "Is null"; 
    } 


    Donate Your Car / Buy Donated Cars in California Charity Thanks, Nishanth Mohan

    • Proposed as answer by Lisa Zhu Thursday, August 23, 2012 2:58 AM
    • Marked as answer by Lisa Zhu Thursday, August 30, 2012 9:03 AM
    Wednesday, August 22, 2012 12:35 PM
  • Try this way:

    stirng query = "SELECT ColumnA FROM MyTable WHERE ColumnB  = 'some condition'";
    //... initialize connection and command and reader classes
    //then do:
    if(reader.Read()) //if there are any value(s)
    {
       if(reader.GetValue(0) != DbNull.Value) //if value in columnA is not null
       {
           //....
       }
    }
    
    //or if reading a column if more rows:
    stirng query = "SELECT ColumnA FROM MyTable";
    //... initialize connection and command and reader classes
    //then do:
    while(reader.Read()) //if there are any value(s)
    {
       if(reader.GetValue(0) != DbNull.Value) //if value in columnA is not null
       {
           //....
       }
    }


    Mitja

    • Proposed as answer by Norkk Wednesday, August 22, 2012 2:19 PM
    • Marked as answer by Lisa Zhu Thursday, August 30, 2012 9:03 AM
    Wednesday, August 22, 2012 2:14 PM

All replies

  • if (reader["YourColumn"] != DBNull.Value)
    {
        somestring.Text = "NotNull";
    }
    else
    {
         somestring.Text = "IsNull";
    }


    //SFP

    • Marked as answer by Lisa Zhu Thursday, August 30, 2012 9:03 AM
    Wednesday, August 22, 2012 12:28 PM
  • Hello, One way would be to use HasRows property.

    if(reader.HasRows)
    {
       //If reader has rows
    }
    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    • Proposed as answer by JohnGrove Wednesday, August 22, 2012 2:42 PM
    Wednesday, August 22, 2012 12:32 PM
  • Another way....

    if (myReader.Read()) 
    {      
    ltlAdditional.Text = "Contains data"; 
    } 
    else 
    {        
    ltlAdditional.Text = "Is null"; 
    } 


    Donate Your Car / Buy Donated Cars in California Charity Thanks, Nishanth Mohan

    • Proposed as answer by Lisa Zhu Thursday, August 23, 2012 2:58 AM
    • Marked as answer by Lisa Zhu Thursday, August 30, 2012 9:03 AM
    Wednesday, August 22, 2012 12:35 PM
  • Try this way:

    stirng query = "SELECT ColumnA FROM MyTable WHERE ColumnB  = 'some condition'";
    //... initialize connection and command and reader classes
    //then do:
    if(reader.Read()) //if there are any value(s)
    {
       if(reader.GetValue(0) != DbNull.Value) //if value in columnA is not null
       {
           //....
       }
    }
    
    //or if reading a column if more rows:
    stirng query = "SELECT ColumnA FROM MyTable";
    //... initialize connection and command and reader classes
    //then do:
    while(reader.Read()) //if there are any value(s)
    {
       if(reader.GetValue(0) != DbNull.Value) //if value in columnA is not null
       {
           //....
       }
    }


    Mitja

    • Proposed as answer by Norkk Wednesday, August 22, 2012 2:19 PM
    • Marked as answer by Lisa Zhu Thursday, August 30, 2012 9:03 AM
    Wednesday, August 22, 2012 2:14 PM