Continually Improving.... let us know how support@devdiamond.net Sign in | Sign up
home articles news blog forum  

 
 


 
Skip Navigation LinksHome > Article > Database Basics :: How to know if no records where returned
Database Basics :: How to know if no records where returned
Abstract :
As a developer you dont want to return error messages to the user, if there was no record returned you can tell the user to check back soon for example.

Views : 6362
Published : Friday, March 22, 2002
By
HyperLink

Avarage Rating :
Page Page 1 of 1

OK, you know how to connect to a database, you need to retrieve data now! so let's start with simple code.

'First we need to make a connection to the database here
Set ConString = Server.CreateObject("ADODB.Connection")
ConString.open 'your connection string goes here
'Then we make the RecordSet
Set ConRS = Server.CreateObject("ADODB.Recordset")
'Ok now it's time to select some records form a table
strSQL = "SELECT * FROM tblProducts"
'Now let's execute this statement
ConRS.open strSQL, ConString


Now let's display the data retrieve, it's simple you just print the Recordset out

While Not ConRS.EOF
Response.write ConRS("ProductName")
ConRS.MoveNext
Wend


This code works good, it loops through all records selected and print the ProductName, the loop stops when ConRS.EOF is True, this means the loop stops when the recordset ends

It works just fine that way, but what if there was nothing on that table you selected, with the first way, there will be no record printed out, hence user may think there is something goes wrong with it
to tell user that there was no record founded and ask him to check back, you need to add If...Else...End if statement, it's simple see how you can do it.

If ConRS.EOF then
      Response.write "There is no record, please check back."
Else
      While Not ConRS.EOF<br>
      Response.write ConRS("ProductName")<br>
      ConRS.MoveNext<br>
      Wend
End If

Can you see it, it is so simple, try yours and play around.



About Author

        Yasir Send Feedback
        Yasir is a .NET expert, with over 5 years experience in Microsoft Technologies, 8 years overall programming experience, he is the owner, founder & primary contributor of Minwar.com, and he also works as IT Director in the hospitality industry.