Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid not updating when update button pressed - newbie

Status
Not open for further replies.

vafishing

Programmer
Jul 18, 2001
35
US
My Update Button is:

Private Sub cmdCustUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCustUpdate.Click
If Not Customer1.GetChanges Is Nothing Then
Try
SqlDataAdapter1.Update(Customer1)
Me.Dispose()
Catch ex As Exception
MsgBox(ex.ToString)
Stop
End Try
End If

I receive an error message: Update requires valid Insert Command when passed datarow collection with new rows.
In my sqldataadapter, I configured it for 2 data tables, customer and account. account_id is a fk in the customer table. customer was chosen as the data set. I read something about command builder and a valid insert command. An explanation and maybe a code example would be helpful here. I am a newbie to this
 
Code:
dim cbu as new sqlcommandbuilder
dim com as new sqlcommand
dim con as new sqlconnection
dim adp as new sqldataadapter
dim dt as new datatable

con.connectionstring = ...
con.open
com.connection = con
com.commandtext = "select * from table"
adp.selectcommand = com
cbu.adapter = adp
adp.insertcommand = cbu.getinsertcommand
adp.updatecommand = cbu.getupdatecommand
adp.deletecommand = cbu.getdeletecommand

adp.fill(dt)
adp.fillschema(dt)

this should work if I didnt forget anything, the only thing you have to make sure of is that you table has a primary key.

Christiaan Baes
Belgium

What a wonderfull world - Louis armstrong
 
I am okay until I enter this code

con.connectionstring= ... it has a problem with the ...

Also, the names of my tables are cutomer and account. Account ID is a fk in customer. In my sqldatadapter1, I used query bulder to link the 2 tables together. So when I get to the code: com.commandtext = "select * from table", should I replace it with com.commandtext = "select * from customer, accounts"

Finally would I be able to put all of the code in the cmd button, including the try catch exception?

Also I used a datagrid because I had trouble figuring out how to put a new form within the existing form. I know about the dataform feature but it does not give you an update feature.

 
that was some sample code, you will need to replace out some values like "..." for your connection string, TABLE in the SQL statement for YOUR table name.

we may be really smart, but we have a long way to clairvoyance.

[thumbsup2]DreX
aKa - Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top