Skip to content
Advertisement

SqlCommandBuilder Update Command is Generating Command but Not Updating Database From DataGridView

I am inheriting a form class (Form1) for Form 2. Form 1 code works without error. In Form 1, I use SqlCommandBuilder(SQL.DBDA).GetUpdateCommand to generate the update command for my Datagrid to pass to SQL data table which again works perfectly and the table is updated successfully. The SQL command text for Form 1 Update is shown here:

Image 1

In Form 2, I write the following for the update command, where the only difference is Selecting the Table shown here:

The command text for this update command is shown here:

Image 2

It is not dissimilar to the successful update command shown in Form1 (image 1). Still, no data is passed to the SQL from the Gridview.

I also tried writing a dynamic Update statement without using the command builder shown below. The text of this statement generates an accurate SQL command but again, nothing passed to the database. This code is shown here:

If anyone has any ideas/ solutions on what I need to do to get the update command working properly, I’d really appreciate it. Thanks!

Added ExecQuery methdod per request below:

Advertisement

Answer

The issue appears to be as I suspected it was. Here’s for code from the form:

In that, you first call ExeQuery and finally call Update on the data adapter and pass the DBDT DataTable. In your ExecQuery method, you have this:

That means that the first code snippet is going to be calling Update and passing a DataTable that was just freshly created and populated. Why would you expect that DataTable to have any changes in it to save? This is an example of why I think DAL classes like this are garbage.

If you’re going to be using a class like that then you should be creating a command builder inside it when you create the data adapter, e.g.

and:

Now there’s no need to call ExecQuery again or create your own command builder. Just call ExecQuery once when you want the data, get the populated DataTable, use it and then call Update on the data adapter and pass that DataTable when it’s time to save.

That said, you don’t even necessarily need to change that class. If you already have an instance and you already called ExecQuery then it already contains the data adapter and the DataTable. You can still create your own command builder if you want but just don’t call ExecQuery again and lose the objects you already had. If you changed that first code snippet to this:

Then it would work, assuming that you are using the same SQLControl instance as you used to get the data in the first place.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement