While sending back parameters getting this error
Error : The Out Parameter must be assigned before control leaves the
current method
Code is
public void GetPapers(string web, out int Id1, out int Id2)
{
SqlConnection conn = new SqlConnection(ConnectionString());
conn.Open();
SqlCommand cmd = new SqlCommand("GetPapers", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@URL", String(web)));
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
Id1 = (int)rdr["ID1"];
Id2 = (int)rdr["ID2"];
}
rdr.Close();
}
calling it as
GetPapers(web, out Id1, out Id2);
Related to this question
You are assigning
Id1andId2inside an if statement and compiler can’t determine if it will be assigned a value at run time, thus the error.You could assign them some default value before the if statement. Something like.
or specify some default values in
elsepart of your condition.An
outtype parameter must be assigned some value, before the control leaves the functions. In your case, compiler can’t determine whether your variables will be assigned or not, because it is being assigned inside anifstatement.See: 5.3 Definite assignment