Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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.

Posted

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.

  • *Experts*
Posted

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

"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
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...