Checking if the recordset is null?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nitindel
    New Member
    • Dec 2007
    • 67

    Checking if the recordset is null?

    Hi All,

    I am working on ASP VBScript...

    How can i check the values of a recordset .i.e whether it contains values or not...??

    Is NULL considered as a string....??

    Actually i want to check on a precondion that if recodset is empty ...i want to take some action
    if not empty then some other action..

    please reply...

    Code taht i have use is:-
    Code:
    SET objDBdbConnection = Server.CreateObject("ADODB.Connection")
    objDBdbConnection.Open Session("ConnectionString")
    SET rsNew =server.createobject("ADODB.Recordset")
    StrSql = "Select IP from rate where docid=1"
    rsNew.open StrSql, objDBdbConnection
    
    if   rsNew.Fields("IP")="NULL" then
        Response.Write("Not Found")
    else
        Response.Write("Found")
    end if

    Thanks :-)
    Last edited by Frinavale; Nov 1 '12, 01:27 PM. Reason: Fixed code tags and formatted code for legibility.
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi Nitindel,

    In order to check if a recordset has anything in it you can simply use the recordset.EOF property. This stands for End Of File and will be True if a recordset is empty. So, using your code:

    Code:
     
    if [b]rsNew.EOF[/b] then
    	Response.Write("Not Found")
    else
    	Response.Write("Found")
    end if
    And note that NULL is not considered as a string and is not the same as "".

    I hope this help and let me know how you get on,

    Dr B

    Comment

    • CroCrew
      Recognized Expert Contributor
      • Jan 2008
      • 564

      #3
      Hello nitindel,

      Checking if the recordset can be done in two ways:

      Like DrBunchman said you can use (EOF):
      [code=asp]
      If (rsNew.EOF) Then
      Response.Write( "RecordSet in empty")
      Else
      Response.Write( "Record(s) are in the RecordSet")
      End If
      [/code]

      Or using the (RecordCount):
      [code=asp]
      If (rsNew.RecordCo unt > 0) Then
      Response.Write( "Record(s) are in the RecordSet")
      Else
      Response.Write( "RecordSet in empty")
      End If
      [/code]

      To check a field within the recordset is “NULL” then us the (IsNull):
      [code=asp]
      If (IsNull(rsNew(" IP"))) Then
      Response.Write( "This Field is Null")
      Else
      Response.Write( "This Field is not Null")
      End If
      [/code]

      Keep in mind that “Null” and “” (blank value) are not one in the same.

      Hope that explains it a bit more~

      Comment

      • markrawlingson
        Recognized Expert Contributor
        • Aug 2007
        • 346

        #4
        if rsNew.Fields("I P")="NULL" then
        -also note that "NULL" is not the same as NULL.

        Comment

        • ritman
          New Member
          • Jun 2010
          • 1

          #5
          if rsNew.Fields("I P")=vbNULL then

          Comment

          • DagJ
            New Member
            • Nov 2012
            • 2

            #6
            You can also use
            If rsNew.State = 1 then 'if open then

            Unfortunately none of the suggestions above will work if the recordset is NULL, i.e. Set rsNew = Nothing.

            Comment

            Working...