I am facing a issue here in reading the values set by a stored procedure in C#.
-
I have a stored procedure which sets variables
@errorCode@errorStrto 0 and “Failed” on failure of a stored procedure. -
I have a C# function which invokes the Stored Procedure and the value is set correctly but I am not able to read the values of
@errorCodeand@errorStrthrough my C# code. -
My code goes as follows
string op = null;
cmd.CommandText = "PopulateAnswers";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@i_xmlreq", SqlDbType.Xml);
param.Value = xmltext;
param.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param);
cmd.Connection = Conn1;
SqlDataReader dataReader = cmd.ExecuteReader();
if (dataReader.HasRows)
{
op = dataReader.GetValue(1).ToString();
if (op.CompareTo("Failed To set Answers") == 0)
{
WriteErrorLog("Failed to execute the stored Procedure");
return "1:Failed to execute stored procedure ";
}
else
{
return "0:success";
}
}
I am getting an error while I am trying to read the data. It says that you are trying to read a value which does not exist. But I am able to see the values properly when I hover over the variable datareader. It’s present under Datareader -> Results view -> [0] -> Non-static .
You need to call dataReader.Read() before you can get the values.