I am calling stored procedure and it does check 2 things and based on that it displays message but if both conditions are good then it updates the table. The table is updated correctly but at the end i get this error: "Object cannot be cast from DBNull to other types". I am not sure why this is happening because i have used similar codes somewhere else. It is failing at this line
userId = Convert.ToInt32(cmd.ExecuteScalar());
here is code behind
protected void Ccs_Click(object sender, EventArgs e)
{
string Ques = secQues.SelectedValue;
int userId = 0;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("myStPR"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@TempID", SqlDbType.VarChar).Value = tempID.Text.Trim();
cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = txtUID.Text.Trim();
cmd.Parameters.Add("@Loc", SqlDbType.VarChar).Value = txtLoc.Text.Trim();
cmd.Connection = con;
con.Open();
userId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
string message = string.Empty;
switch (userId)
{
case -1:
message = "some message.";
break;
case -2:
message = "some message";
break;
default:
message = "Registration successful. You will receive an email once your application is approved. Thanks.";
break;
}
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
}
}
cmd.ExecuteScalar()?