DataTable dt= new Datatable();
try {
SqlCommand Cmd = new SqlCommand("sp_getData",SqlCon);
SqlCommand.CommandType= CommandType.StroedProcedure;
SqlCon.Open();
sqlDataReader dr= cmd.ExecuteReader();
dt.Load(dr);
SqlCon.Close();
}
catch(Exception ex) {
}
finally{
dt.Dispose() //
}
return dt;
Is this code legitimate ?... My method is returning that datatable object , so after calling dispose will that retain the value ??.. please explain..
this will give you what you want:
public DataTable getDataTable()
{
using (SqlConnection sqlCon = new SqlConnection("connectionString"))
using (SqlCommand cmd = new SqlCommand("sp_getData", sqlCon))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
sqlCon.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(dr);
return dt;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return null;
}
}