how to trap/deal nullRefException for cmd.ExecuteScalar w/o try-c

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

'Shmo' is a unique word in tbl1. If it exists in tbl1, then I write it to
the console.

cmd.commandType = CommandType.Text
cmd.CommandText = "Select fldx From tbl1 Where fldx = 'Shmo'"
console.writeline(cmd.ExecuteScalar.ToString)

If 'Shmo' does not exist in tbl1 - I get a nullreferenceException. I deal
with this exception in a try-catch-finally block. My question is if there is
a way to deal with the non-existence of 'Shmo' besides try-catch.

I tried

if cmd.executeScalar.ToString.Length > 0 Then console.writeline...

but I still got an exception when there was no 'Shmo' to be found. Is there
a better way to deal with this than just Try-Catch?

Thanks,
Rich
 
Thanks. Now I will have to experiment with nullable types and their
implementation. I guess .Net 2.0 has the nullable type feature? How could I
use that to trap for the null return I get from

....
cmd.CommandText = "Select fldx From tbl1 Where fldx = 'Shmo'"
console.writeline(cmd.ExecuteScalar.ToString)

could I do something like:

nullable<cmd.ExecuteScalar.ToString>

or how about

nullable<cmd.ExecuteNonQuery>

Thanks,
Rich
 
Just learning here what do you mean 'Shmo' is a unique word in tbl1

I must be missing something in here.

Thanks for any info .

SA
 
In a table with n rows and one column, one of those rows contains/may contain
the text 'shmo'. At least I am searching to see if any of the rows contains
the text 'shmo'.

Select * from tblx Where col1 = 'shmo'

If there is no 'shmo' I get the nullreferenceException using ADO.Net. With
com ADO I would use an ADODB Recordset to retrieve the record. Then I could
do something like this in the client

Set RS = cmd.Execute
If RS.EOF then... that means there was no 'shmo'

I am sure that ADO.Net must have a similar mechanism for dealing with nulls
rather than doing the Error out thing. But what is that mechanism?
 
Rich said:
Hello,

'Shmo' is a unique word in tbl1. If it exists in tbl1, then I write it to
the console.

cmd.commandType = CommandType.Text
cmd.CommandText = "Select fldx From tbl1 Where fldx = 'Shmo'"
console.writeline(cmd.ExecuteScalar.ToString)

If 'Shmo' does not exist in tbl1 - I get a nullreferenceException. I deal
with this exception in a try-catch-finally block. My question is if there
is
a way to deal with the non-existence of 'Shmo' besides try-catch.

Replace the last line with something like the following (in C#, but it
should be easy to convert to VB if that's what you're using):

object result = cmd.ExecuteScalar();
if (result == null) {
console.writeline("Shmo does not exist in table");
}
else {
string shmo = (string)result; // assuming that fldx is a string field
console.writeline(shmo);
}

Chris Jobson
 
have you tried,

if not ( cmd.ExecuteScalar is nothing ) then
cmd.ExecuteScalar.ToString
Endif
 
Thanks all for your replies. The suggestions worked out. Where I was having
a problem was getting the correct syntax for using the Nothing keyword

If Not (cmd.ExecuteScalar Is Nothing) then

Using Dim obj As Object = cmd.ExecuteScalar

and checking if the object is null also worked out.

Thanks again all for the help.

Rich
 
Back
Top