ConnectionlHelper conn = new ConnectionlHelper();
conn.OpenConn();
try
{
SqlCommand cmd = new SqlCommand("sp_fetchExecutionPlan", conn.oConnection);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
dgvExecutionPlan.Rows.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
dgvExecutionPlan.Rows.Add();
dgvExecutionPlan.Rows[i].Cells[0].Value = dt.Rows[i]["ID"].ToString();
dgvExecutionPlan.Rows[i].Cells[1].Value = dt.Rows[i]["StartTime"].ToString();
dgvExecutionPlan.Rows[i].Cells[2].Value = dt.Rows[i]["EndTime"].ToString();
dgvExecutionPlan.Rows[i].Cells[3].Value = dt.Rows[i]["Note"].ToString();
if (Convert.ToBoolean(dt.Rows[i]["Status"]) == Convert.ToBoolean(0))
{
dgvExecutionPlan.Rows[i].Cells[4].ValueType = typeof(string);
dgvExecutionPlan.Rows[i].Cells[4].Value = "Not Executed";
}
else
{
dgvExecutionPlan.Rows[i].Cells[4].ValueType = typeof(string);
dgvExecutionPlan.Rows[i].Cells[4].Value = "Executed";
}
}
//conn.Close();
dt.Clear();
}
}
catch (Exception ex)
{
lblExeplan2.Text = "Error Loding Execution Plan: " + ex.Message.ToString();
}
finally
{
conn.Close();
}
you can clear the datatable in the final block and close the connection. other than you can close the connection when the for loop ends. but closing the connection in the final block is a good practice.
