0

I'm trying to save changes from my DataGridview back into the SQL Server 2008. I have the following code but it only updates the DB the first time the button is clicked. Where am I overlooking something?

private void btnDone_Click_1(object sender, EventArgs e)
{
    try
    {
       SqlConnection con = new SqlConnection(constr);
       con.Open();

       adapter.SelectCommand = new SqlCommand("SELECT * FROM trips", con);
       SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
       adapter.Fill(dt);
       adapter.Update(dt);

       con.Close();

       MessageBox.Show("Changes Saved", "Updating...", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception)
    {
       throw;
    }
    finally { lblCount.Text = "Total Records Displayed: " + dgvTrips.Rows.Count.ToString(); }
}
Emile Cloete
  • 143
  • 1
  • 2
  • 10

1 Answers1

0

Worked by declaring a global SqlDataAdapter

Emile Cloete
  • 143
  • 1
  • 2
  • 10