mulefeathes Posted September 4, 2004 Posted September 4, 2004 I have been able to add a new row to my access database. I am using a form designed in VB.net. When I try to edit and save changes I receive error "Primary Key is already used". How can I just update a record already in the table???? Dim tblevents As DataTable tblevents = Dsspecial1.Tables("special events") Dim dr As Data.DataRow dr = tblevents.NewRow() Me.OleDbConnection1.Open() dr.Item("Name") = Me.txtname.Text dr.Item("Address") = Me.txtaddress.Text dr.Item("City") = Me.txtcity.Text dr.Item("State") = Me.txtstate.Text dr.Item("Zip") = Me.Txtzip.Text dr.Item("Phone") = Me.txthphone.Text dr.Item("workphone") = Me.txtwphone.Text dr.Item("package") = Me.cbopackage.Text dr.Item("money") = Me.txtmoney.Text dr.Item("typeofevent") = Me.txttype.Text dr.Item("date") = Me.txtdate.Text dr.Item("photo number") = Me.txtphotonumber.Text dr.Item("cash") = Me.Radcash.Checked dr.Item("check") = Me.radcheck.Checked tblevents.Rows.Add(dr) Me.dbspecial.Update(Dsspecial1.GetChanges(DataRowState.Added)) Me.Dsspecial1.Reset() locktxtboxes() This the code I used to add a record. Quote
Cassio Posted September 4, 2004 Posted September 4, 2004 Thats because you're trying to add the row your are editing. You just need to edit the row and then update the database. You can use the index of the row or use the select() method of the datatable to find the row you're trying to edit. Quote Stream of Consciousness (My blog)
mulefeathes Posted September 4, 2004 Author Posted September 4, 2004 I understand that but how do I edit using the code I submited what should I change to edit??? Thats because you're trying to add the row your are editing. You just need to edit the row and then update the database. You can use the index of the row or use the select() method of the datatable to find the row you're trying to edit. Quote
*Experts* Nerseus Posted September 5, 2004 *Experts* Posted September 5, 2004 Are you assuming you've already got data in Dsspecial1? If so, you can update the first row by changing: dr = tblevents.NewRow() to dr = tblevents.Rows(0) And then change line: tblevents.Rows.Add(dr) to dr.EndEdit() If you want other rows, you'll have to let us know how you would find them in your DataSet - there are ways to search for rows, loop through rows, and more. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
mulefeathes Posted September 6, 2004 Author Posted September 6, 2004 Updating Yes data is already in the Dsspecial1. I add rows from the form using the above code. I want to be able to look at a record then strike edit button and change current information then save back to the table. Thanks I will try you code. Are you assuming you've already got data in Dsspecial1? If so, you can update the first row by changing: dr = tblevents.NewRow() to dr = tblevents.Rows(0) And then change line: tblevents.Rows.Add(dr) to dr.EndEdit() If you want other rows, you'll have to let us know how you would find them in your DataSet - there are ways to search for rows, loop through rows, and more. -nerseus Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.