I have a using statement to initialize adapter and Fill()
. I want to update adapter in different function but it gives me error since connection closed. How to do this with using or should I not use using in this case?
SqlDataAdapter adt;
DataTable dt;
private void myTestAdapterUpdate()
{
using (SqlConnection connection = new SqlConnection(conString))
{
dt = new DataTable();
string query = "SELECT * FROM dbo.Vendor_GUI_Test_Data";
SqlCommand cmd = new SqlCommand(query, connection);
connection.Open();
adt = new SqlDataAdapter(cmd);
adt.Fill(dt);
DataRow toInsert = dt.NewRow();
toInsert[0] = "MYTESTVENDOR2";
toInsert[1] = "4";
toInsert[2] = "89";
toInsert[3] = "89";
dt.Rows.InsertAt(toInsert, 0);
DataRow r = dt.Rows[3];
r.Delete();
SqlCommandBuilder builder = new SqlCommandBuilder(adt);
}
}
private void applyTestAdapterUpdate()
{
adt.Update(dt); //error here
}