Link to home
Start Free TrialLog in
Avatar of gandalf97
gandalf97

asked on

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.

    

'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

Open in new window


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
Avatar of drypz
drypz
Flag of Philippines image

Hi! Try to add MissingSchemaAction. Something like


gdaMyDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
gdaMyDataAdapter.Fill(gdtMyDataTable)
MyDataGridView.DataSource = gdtMyDataTable
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gandalf97
gandalf97

ASKER

@drypz:

I attempted to respond earlier (yesterday) but a power outage at work caused a severe drop in productivity.  :-P

I was researching the MissingSchemaAction methods and am still fuzzy on what they do, how they work and whether or not they will help me.  Can you please elaborate?


@grave:

Your explanation of the SQLCommandBuilder using the current eSelectCommand from the DataAdapter is clear and is what I read from MS.  I understand this (to a point) as well as those 3 command objects being generated only when they are needed.  *HOWEVER*  Several articles/examples have you explicitly call the .GetUpdateCommand method and it *DOES* generate a command object.  The CommandText is wierd but it is there.  Are you saying that when the Update method hits a delete/insert, the Inaert/Delete Command object(s) are then generated, executed and destroyed?  If you don't need those "GetCommand" methods, what are they there for?

As for the RowValidating suggestion, I tried that and that really helps.  I just couldn't find the right event to do that.  The one wrinkle is that the value for OtherKey gets stuffed in the last (empty) row as well and is then inserted in the database.  While I can run a Delete query to go back and clean up, I'd rather not have to.  Is there a way to prevent the OtherKey value from going into that row?

Thanks!
Eric
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I haven't forgotten about this.  I was order to switch to another task for a couple of days and had to shelve checking out the suggestions. I appreciate the help.
I split the points between the various answers because I felt the first answer needed to be clarified (which it was) and the one about which event to use really put it all together for me.

Thanks for drypz for trying to help.