468,283 Members | 1,957 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,283 developers. It's quick & easy.

Help Deleting from DataBase

10
Can anyone explain why this code doesnt delete from the database. Some times it works and some times it errors i am confused.

Expand|Select|Wrap|Line Numbers
  1. Private Sub DeleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBtn.Click
  2.  
  3.     Dim connStr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
  4.     "Data Source = G2G.mdb"
  5.     Dim oDataTable As New DataTable
  6.     Dim squlStr As String = "SELECT * From Staff"
  7.     Dim oleDbStaffAdapter As New OleDb.OleDbDataAdapter(squlStr, connStr)
  8.  
  9.         oleDbStaffAdapter.Fill(oDataTable)
  10.         Dim StaffDeleteRow As DataRow
  11.  
  12.         oDataTable.Rows.Item(Val(StaffnoTxt.Text)).Delete()
  13.  
  14.         Dim changes As Integer
  15.         Dim commandbuilder As New OleDb.OleDbCommandBuilder(oleDbStaffAdapter)
  16.  
  17.         changes = oleDbStaffAdapter.Update(oDataTable)
  18.  
  19.  
  20.     End Sub
Thanks
Dec 7 '07 #1
3 1007
hathan
10
Hi i am getting An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll. Some times my code works and some times it doesn't. Its realy wierd. It is erroring on changes = oleDbStaffAdapter.Update(aDataTable) can anyone please help me.

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBtn.Click
  2.  
  3.     Dim connStr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
  4.     "Data Source = G2G.mdb"
  5.     Dim aDataTable As New DataTable
  6.     Dim squlStr As String = "SELECT * From Staff"
  7.     Dim oleDbStaffAdapter As New OleDb.OleDbDataAdapter(squlStr, connStr)
  8.  
  9.  
  10.       Try
  11.             aDataTable.Clear()
  12.             StaffnoTxt.DataBindings.Clear()
  13.             FnameTxt.DataBindings.Clear()
  14.             LastnameTxt.DataBindings.Clear()
  15.             NoTxt.DataBindings.Clear()
  16.             PcodeTxt.DataBindings.Clear()
  17.             DoornoTxt.DataBindings.Clear()
  18.  
  19.             oleDbStaffAdapter.Fill(aDataTable)
  20.             Dim MyNewRow As DataRow = aDataTable.NewRow
  21.  
  22.             MyNewRow("Staff_No") = StaffnoTxt.Text
  23.             MyNewRow("First_Name") = FnameTxt.Text
  24.             MyNewRow("Surname") = LastnameTxt.Text
  25.             MyNewRow("Tel_No") = NoTxt.Text
  26.             MyNewRow("Post_Code") = PcodeTxt.Text
  27.             MyNewRow("House") = DoornoTxt.Text
  28.  
  29.             aDataTable.Rows.Add(MyNewRow)
  30.  
  31.  
  32.             Dim changes As Integer
  33.             Dim commandbuilder As New OleDb.OleDbCommandBuilder(oleDbStaffAdapter)
  34.             changes = oleDbStaffAdapter.Update(aDataTable)
  35.  
  36.             AddLbl.Text = "New Staff Added"
  37.  
  38.  
  39.         Catch ex As InvalidOperationException
  40.             MsgBox(ex.Message)
  41.         End Try
  42. End Sub
Thanks
Jonathan
Dec 7 '07 #2
kenobewan
4,871 Expert 4TB
I thought it would have been commandbuilder.Update(aDataTable), but its late and the screen has become blurred :).
Dec 8 '07 #3
kenobewan
4,871 Expert 4TB
I note the same code is coming up multiple question and that you are basically multiposting under the guise of different questions. I also find it difficult to believe that this is a university course, although some unis will offer anything if they can find a student.

Your questions appears to be a Homework Assignment and you should make a proper attempt at assignment/program. This question will now be closed and you will have to post a new question when you're ready. I strongly recommend reading the posting guidelines.

MODERATOR
Dec 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by tabani | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.