0

I have somehow a problem to update my SQL Server database. I got an "Update" button so the user can change values manually in a DataGridView and then should click a button to save the changes to the database. I use the "Update" command for that but don't know how to manage it.

The button:

 private void update_Click(object sender, EventArgs e)
 {     
     sqlCmdBuilder = new SqlCommandBuilder(sqlDatAdapter);
     dataGridView1.DataSource = datTable;
     sqlDatAdapter.Fill(datTable);
     sqlDatAdapter.Update(datTable);    
 }

The database gets loaded via another function. In my code I get a NullReferenceException.

How can I catch the existing DataGrid and then register the users changes?

When I define the SqlDatAdapter in the method then the exception doesn't occur anymore but it does absolutely nothing.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MKX2015
  • 155
  • 1
  • 2
  • 13
  • On which line exactly? Did you debug your code? And, [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Jan 28 '15 at 08:11
  • I get it in "sqlDatAdapter.Fill(datTable);" Yes I know what a NullReferenceException is but I don't know how to solve it in that case. I tried some things but it didn't work. – MKX2015 Jan 28 '15 at 08:14

1 Answers1

0

Solved. I used

 DataTable datTable = dataGridView1.DataSource as DataTable;

and now the Exception isn't there anymore.

MKX2015
  • 155
  • 1
  • 2
  • 13