It appears that you can only retrieve an Output parameter from a SQL Server
stored procedure when using the ExecuteNonQuery of the SqlCommand class, and
cannot use the ExecuteReader() method. In the code sample below, it bombs
on the line with three *** when using the ExecuteReader, but works just fine
when you use the ExecuteNonQuery . When using the ExecuteReader, the
Output parameter appears to return a value of Null. The stored procedure
was not changed inbetween.
Suggestions? Thanks in advance.
Mark
String strConn =
ConfigurationSe ttings.AppSetti ngs["myConnecti on"].ToString();
SqlConnection sqlConn = new SqlConnection(s trConn);
SqlCommand sqlComm = new SqlCommand("p_m y_proc", sqlConn);
sqlComm.Command Type = CommandType.Sto redProcedure;
sqlComm.Paramet ers.Add(new SqlParameter("@ bitSomeResult", SqlDbType.Bit)) ;
sqlComm.Paramet ers["@bitSomeResult "].Direction = ParameterDirect ion.Output;
sqlConn.Open();
SqlDataReader dr = sqlComm.Execute Reader();
//sqlComm.Execute NonQuery();
String strSomeResult =
sqlComm.Paramet ers["@bitSomeResult "].Value.ToString (); //***
if (strSomeResult. ToLower() == "True".ToLower( ))
{
//Do something
}
else
{
//Do something else
}
sqlConn.Close() ;
stored procedure when using the ExecuteNonQuery of the SqlCommand class, and
cannot use the ExecuteReader() method. In the code sample below, it bombs
on the line with three *** when using the ExecuteReader, but works just fine
when you use the ExecuteNonQuery . When using the ExecuteReader, the
Output parameter appears to return a value of Null. The stored procedure
was not changed inbetween.
Suggestions? Thanks in advance.
Mark
String strConn =
ConfigurationSe ttings.AppSetti ngs["myConnecti on"].ToString();
SqlConnection sqlConn = new SqlConnection(s trConn);
SqlCommand sqlComm = new SqlCommand("p_m y_proc", sqlConn);
sqlComm.Command Type = CommandType.Sto redProcedure;
sqlComm.Paramet ers.Add(new SqlParameter("@ bitSomeResult", SqlDbType.Bit)) ;
sqlComm.Paramet ers["@bitSomeResult "].Direction = ParameterDirect ion.Output;
sqlConn.Open();
SqlDataReader dr = sqlComm.Execute Reader();
//sqlComm.Execute NonQuery();
String strSomeResult =
sqlComm.Paramet ers["@bitSomeResult "].Value.ToString (); //***
if (strSomeResult. ToLower() == "True".ToLower( ))
{
//Do something
}
else
{
//Do something else
}
sqlConn.Close() ;
Comment