I have a stored procedure it returns a name so i need to get the name in in C#
so i just execute the Sp from C# and read the out put using ExecuteReader()
like below
using (var objCommand = new SqlCommand("SpName", objConnection))
{
objCommand.Parameters.AddWithValue("@Param1", Param);
objCommand.Parameters.AddWithValue("@Purpose", Purpose);
objConnection.Open();
objCommand.CommandType = CommandType.StoredProcedure;
using (var reader = objCommand.ExecuteReader())
{
while (reader.Read())
{
objemailsend.Name = Convert.ToString(reader["Name"]);
}
}
}
objConnection.Close();
It gives an exception
Index Out of range exception
I am sure that the index names are same i mean the Sp also returns the same name
Name
----
name1
like above
then i tried something like below and now the exception disappears and it returns a numeric value i don't know from where the value coming from and it is not same as my SP result
objexample.Name = reader[0].ToString();
also tried
using (var reader = objCommand.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
objemailsend.Name = Convert.ToString(reader["Name"]);
}
}
}
But the same error
Can you somebody help me to solve my issue.