I have the following code and I was wondering if anyone knew the correct way to handle this.
SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["someConnectionString"].ConnectionString);
SqlCommand objComm = new SqlCommand("usp_someStoredProcedure", objConn);
objComm.CommandType = CommandType.StoredProcedure;
objComm.Parameters.AddWithValue("@Variable1", VOne);
objComm.Parameters.AddWithValue("@Variable2", VTwo);
objConn.Open();
using (IDataReader dr = objComm.ExecuteReader(CommandBehavior.CloseConnection))
{
//do stuff
}
Now, let's say the stored procedure returns nothing, is there a method to handle this?
Normally the section you have marked with //do stuff would contain a
if (dr.Read())
{
// do stuff
}
or a
while (dr.Read())
{
// do stuff
}
The .Read() check ensures that you're only acting if it returned data.