I am creating a crud gridview webpage and I am using the "using" statement in ASP.NET in my code because I learned that it automatically converts the code that is inside it so I won't have to use connect.Close();
But still, I am getting an error:
System.InvalidOperationException: The connection was not closed. The connection's current state is open.
I tried to put connection.Close();
but still, the same error occurs.
This is my code. Can anyone please help me solve the problem? Thank you so much
void PopulateGridView()
{
using (connect)
{
connect.Open();
adapter = new SqlDataAdapter("SELECT * FROM RetailInfo", connect);
table = new DataTable();
adapter.Fill(table);
}
if(table.Rows.Count > 0)
{
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
}
else
{
table.Rows.Add(table.NewRow());
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
RetailInfoGridView.Rows[0].Cells.Clear();
RetailInfoGridView.Rows[0].Cells.Add(new TableCell());
RetailInfoGridView.Rows[0].Cells[0].ColumnSpan = table.Columns.Count;
RetailInfoGridView.Rows[0].Cells[0].Text = "No record Found";
}
}