C# - What is the return value for SqlCommand.ExecuteScalar?
I've got the following code...
cmd = new SqlCommand(strSQL, conn);
cmd.Connection.Open();
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?
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
}
else
{
// record found, do proper casting like:
// int x = (int)obj;
// string s = (string)obj;
}
object obj = cmd.ExecuteScalar();
if (obj == null)
{
// record not found, do something
}
else
{
// record found, do proper casting like:
// int x = (int)obj;
// string s = (string)obj;
}