I have a stored procedure which returns flag values based on certain conditions. The stored procedure does not return a NULL
value, it returns 1, 2, or 3 based on some condition. But when I debug, my ADO.NET code throws an exception because ExecuteScalar
is returning NULL
.
Here is my ADO.NET code:
SqlCommand cmd = new SqlCommand("sp_checkifexists", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name",name);
cmd.Parameters.AddWithValue("@Regno",regno);
cmd.Parameters.Add("@flag", System.Data.SqlDbType.VarChar, 40);
cmd.Parameters["@flag"].Direction = System.Data.ParameterDirection.Output;
con.Open();
// Error occcurs here
int NoOfRows = Convert.ToInt16(cmd.ExecuteScalar());
con.Close();
int flag = (int)cmd.Parameters["@flag"].Value;
return flag;
Any help would be much appreciated!