Link to home
Start Free TrialLog in
Avatar of josephdaviskcrm
josephdaviskcrmFlag for United States of America

asked on

C# - What is the return value for SqlCommand.ExecuteScalar?

I've got the following code...

cmd = new SqlCommand(strSQL, conn);
strScalar = cmd.ExecuteScalar();                   <======= Line causing error

What is the return type of the value of this type of command?  Am I using ExecuteScalar() correctly?  How do I make it into a string that will feed into this variable?
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you always have to check if the return value is null or not:
object obj = cmd.ExecuteScalar();
if (obj == null)
     // record not found, do something
     // record found, do proper casting like:
     // int x = (int)obj;
     // string s = (string)obj;