object cannot be cast from dbnull to other types[ADO.net]
I have a stored procedure which will return flag values based on certain
conditions.The stored procedure does not return a null value,It returns
1,2,3 based on condition .But in my ADO.Net code,when I DEBUG, it throws
an exception and the "execute scalar" 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!
No comments:
Post a Comment