Trapping error

  • Thread starter Thread starter Zahid
  • Start date Start date
Z

Zahid

Hi,

Im writing an SQL piece of code that throws an exception
if the value im passing in the SQL Query doesnt exist (ie
when serching the database table the value doesnt exist
in database table).

What I want to do is get a 0 returned to indicate to me
that it does not exist in database table without the
exception being thrown.

Any ideas how to do that? My code is below:

gMyCommand.CommandText =
"Select link from listHdrs where listNo = " & gListMenu

gRdr = gMyCommand.ExecuteReader
gRdr.Read()
gListMenuLink = gRdr.GetInt32(0)
gRdr.Close()


Thanks in advace.
 
1. Use If...Else to check gRdr.Read() is true or false,
false means no records any more.
2. You may use ExecuteScaler() instead of ExecuteReader()
since you just want the first column value in first row.
3. You can add Try...Catch to handle any possible exceptions.

gMyCommand.CommandText = "Select link from listHdrs where listNo = " &
gListMenu
gRdr = gMyCommand.ExecuteReader()
if gRdr.Read() then
gListMenuLink = gRdr.GetInt32(0)
else
gListMenuLink = 0
endif
gRdr.Close()
 
Back
Top