M
mimi
Hi
Please help me out, I can't find a way to close a sqldatareader when error
occur at statement cmd.ExecuteReader(). I can't close it in catch because
it is local in try scope and I can't declare it outside try scope either
since we have to call cmd.executeReader to create sqldatareader
public string GetLogs(int logID)
{
string notes = String.Empty;
// cannot declare like SqlDataReader dr;
try
{
SqlCommand cmd = new SqlCommand("GetLogs", oConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ID",SqlDbType.Int).Value = logID;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
notes = dr["Notes"].ToString();
}
dr.Close();
}
catch(SqlException ex)
{
//can't call dr.Close() because it's not global
}
return notes;
}
Please help me out, I can't find a way to close a sqldatareader when error
occur at statement cmd.ExecuteReader(). I can't close it in catch because
it is local in try scope and I can't declare it outside try scope either
since we have to call cmd.executeReader to create sqldatareader
public string GetLogs(int logID)
{
string notes = String.Empty;
// cannot declare like SqlDataReader dr;
try
{
SqlCommand cmd = new SqlCommand("GetLogs", oConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ID",SqlDbType.Int).Value = logID;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
notes = dr["Notes"].ToString();
}
dr.Close();
}
catch(SqlException ex)
{
//can't call dr.Close() because it's not global
}
return notes;
}