Checking to see if SQLReader is null

Last post 11-15-2009 10:19 AM by rtcary. 3 replies.

Sort Posts:

  • Checking to see if SQLReader is null

    11-14-2009, 10:50 PM
    • Member
      96 point Member
    • rtcary
    • Member since 10-12-2009, 2:50 PM
    • Posts 149

    I have

    SQLReader clmReader;

    clmReader.Read();

    if (clmReader.IsDBNull )

    The IsDBNull is not correct; what did I leave out?

    Todd

  • Re: Checking to see if SQLReader is null

    11-15-2009, 3:27 AM
    Answer
    • Participant
      876 point Participant
    • Babunareshnarra
    • Member since 07-12-2009, 7:10 AM
    • Hyderabad
    • Posts 179

    if(clmReader.HasRows)

    This serves your purpose.

    Hope this helps.

    Regards

    Babu Naresh Narra

    Remember to click “Mark as Answer” on the post If you get answer from my post(s) !
  • Re: Checking to see if SQLReader is null

    11-15-2009, 6:16 AM
    Answer
    • All-Star
      18,854 point All-Star
    • raghav_khunger
    • Member since 08-18-2008, 12:25 PM
    • Delhi, India
    • Posts 3,449
    • TrustedFriends-MVPs

    Hi, rtCary

    Yes you can use if (dataReader.HasRows). Also i will suggest to wrap your code under "using" . Below is the sample code

                using (SqlConnection connection = new SqlConnection("xx--Your Connection String—xx"))
                {
                    using (SqlCommand command = new SqlCommand("xx-Your Command--xx", connection))
                    {
                        connection.Open();
                        using (SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            if (dataReader != null)
                                if (dataReader.HasRows)
                                {
                                   //your logic
                                }
                        }
                    }
                }


    The using statement guarantees that resources are properly cleaned up and that they're cleaned up in the most timely manner.

    Raghav CodeASP.NET Community | My Blog | jQuery Intellisense in Visual Studio 2008




    "Success doesn't come to you…you go to it."--Marva Collins

    "Failure is success if we learn from it." Malcolm Forbes

    "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous


  • Re: Checking to see if SQLReader is null

    11-15-2009, 10:19 AM
    • Member
      96 point Member
    • rtcary
    • Member since 10-12-2009, 2:50 PM
    • Posts 149

    Thank you both!

    Todd


Page 1 of 1 (4 items)