Question : SQLCommandBuilder - GetUpdateCommand, GetInsertCommand and GetDeleteCommand  Questions

As I posted in my previous question I have a VB 2005 app which displays 3 fields from a SQL-Server table in a DataGridView.  2 columns are there but not visible, including the primary key.  While the below code "works" as far as committing the changes in the DataGridView control to the database when I click the Save button, I don't understand why.  I looked at the CommandText for the UpdateCommand object generated by the GetUpdateCommand method and while it was readable, I couldn't tell how it was getting the parameters populated.  I also couldn't tell how it would insert or delete records but it seemed to.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
    

'Declared in a "Globals" Module:
Public gdaMyDataAdapter As New SqlDataAdapter
Public gdtMyDataTable As New DataTable

'Populate the Grid...
Public Sub PopulateGrid()

Dim sSQL As String
Dim cn As New SqlConnection(MyConnectionString)
cn.Open()

sSQL = "SELECT MyKey, col1, col2, col3, OtherKey FROM MyTable WHERE OtherKey = 1234"

Dim cmdSelect As New SqlCommand(sSQL, cn)
gdaMyDataAdapter.SelectCommand = cmdSelect

'Not sure I need to do these here or if they conflict with the ones in "save"
Dim MyCommandBuilder As New SqlCommandBuilder(gdaMyDataAdapter)
gdaMyDataAdapter.UpdateCommand = MyCommandBuilder.GetUpdateCommand(True)

gdaMyDataAdapter.Fill(gdtMyDataTable)
MyDataGridView.DataSource = gdtMyDataTable

End Sub

'Save.  Called by a button click event procedure....

Public Sub GridSave()

Dim MyCommandBuilder As New SqlCommandBuilder(gdaMyDataAdapter)

MyCommandBuilder.GetUpdateCommand()

'Below two calls seem to do NOTHING.
MyCommandBuilder.GetDeleteCommand()
MyCommandBuilder.GetInsertCommand()

'As a test, I was able to override the CommandText for the DataAdapter's UpdateCommand object like this:
gdaMyDataAdapter.UpdateCommand.CommandText = "<Various Tweaks of the Edited UPDATE query string>"

iRows = gdaMyDataAdapter.Update(gdtMyDataTable)
gdtMyDataTable.AcceptChanges()

End Sub


I would like to know why the GetInsertCommand and GetDeleteCommand don't set the corresponding DataAdapter command objects but the GetUpdate seems to cover it all (Insert/Delete/Update) when I make many changes via the grid with a single call  (da.Update(dt)).

Where I am going is that I am having trouble getting the Update to Insert anything other than NULL for "OtherKey".  MyKey is in the underlying table as an identity column and it is invisible in the grid.  I have the user add rows directly to the grid by typing values in col1, col2 and col3.  I have "OtherKey" stored in a TextBox on Form1.  I'd like to have "OtherKey" be invisible in the grid and get the value from a variable that was set from the TextBox.  No matter how I tweak the UPDATE all I seem to be able to send my table is a NULL.

If I'm unclear or you need any more info, please let me know.

Regards,
Eric

Answer : SQLCommandBuilder - GetUpdateCommand, GetInsertCommand and GetDeleteCommand  Questions

First of all, this line is not needed:

gdaMyDataAdapter.UpdateCommand = MyCommandBuilder.GetUpdateCommand(True)

...that's because the the SqlCommandBuilder takes the current SelectCommand from the DataAdapter that is passed to it to generate the required InsertCommand, DeleteCommand, and UpdateCommand.  Since the DataAdapter was passed by reference, the changes made inside the CommandBuilder are now visable to the rest of the program.

Likewise the following are not needed:

MyCommandBuilder.GetUpdateCommand()

'Below two calls seem to do NOTHING.
MyCommandBuilder.GetDeleteCommand()
MyCommandBuilder.GetInsertCommand()

... for the same reason.  You pass the DataAdpater to the constructor of the CommandBuilder, and all of the required commands are automagically set inside the CommandBuilder.

Random Solutions  
 
programming4us programming4us