How can I update my access database via VB.net using this code

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 17
Reputation: Mr Brownstone is an unknown quantity at this point 
Solved Threads: 1
Mr Brownstone's Avatar
Mr Brownstone Mr Brownstone is offline Offline
Newbie Poster

How can I update my access database via VB.net using this code

 
0
  #1
Mar 13th, 2008
Hi everyone, I nearly finished my project but have come across an error. I can't update the database, it's the only error that comes up. I tried a few things but couldn't get my head around it. Any Help will be greatly appreciated.

Here is the main snippet of code with the error.

  1. Imports System.Data.OleDb
  2.  
  3. Public Class Form2
  4.  
  5. Dim column1string As String
  6. Dim column2string As String
  7. Dim column3string As String
  8. Dim column4string As String
  9. Dim Count As Integer
  10. Dim Count2 As Integer
  11. Dim Count3 As Integer
  12. Dim Count4 As Integer
  13. Dim playerquerysql As String
  14.  
  15.  
  16. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  17.  
  18. 'create constant and things
  19. Dim con As New OleDb.OleDbConnection
  20. Dim cnn As New OleDb.OleDbConnection
  21. Dim ds_G As New DataSet
  22. Dim ds_Y As New DataSet
  23. Dim ds_T As New DataSet
  24. Dim dt_G As New DataTable
  25. Dim da_y As New OleDb.OleDbDataAdapter
  26. Dim da_g As New OleDb.OleDbDataAdapter
  27. Dim da_t As New OleDb.OleDbDataAdapter
  28. Dim sql As String
  29. Dim iStGreen As Integer
  30. Dim iStYellow As Integer
  31. Dim vGames(4) As String
  32. vGames(0) = ""
  33. vGames(1) = "Snap"
  34. vGames(2) = "Cribbage"
  35. vGames(3) = "Spilikins"
  36. vGames(4) = "Scrabble"
  37. Dim iGames As Integer
  38. Dim i As Integer
  39. Dim j As Integer
  40. Dim g As Integer
  41.  
  42.  
  43. iStYellow = 18
  44. iStGreen = 18
  45. iGames = 4
  46.  
  47.  
  48. Dim aGID As String = String.Concat(iStGreen, iGames)
  49. Dim aYId As String = String.Concat(iStYellow, iGames)
  50.  
  51.  
  52. con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = db.mdb"
  53.  
  54. con.Open()
  55.  
  56. sql = "SELECT * FROM Pupils"
  57. da_y = New OleDb.OleDbDataAdapter(sql, con)
  58. da_y.Fill(ds_Y, "Pupils")
  59.  
  60. sql = "SELECT * FROM Pupils_1"
  61. da_g = New OleDb.OleDbDataAdapter(sql, con)
  62. da_g.Fill(ds_G, "Pupils_1")
  63.  
  64. sql = "SELECT * FROM tmpFixtures"
  65. da_t = New OleDb.OleDbDataAdapter(sql, con)
  66. da_t.Fill(ds_T, "tmpFixtures")
  67.  
  68. For g = 5 To 8
  69. For i = 0 To 17
  70. If ds_Y.Tables("Pupils").Rows(i).Item(g) <> True Then
  71. For j = 0 To 17
  72.  
  73. If ds_G.Tables("Pupils_1").Rows(j).Item(g) <> True Then
  74. If ds_Y.Tables("Pupils").Rows(i).Item("Class_Y") <> ds_G.Tables("Pupils_1").Rows(j).Item("Class_G") Then
  75. If ds_Y.Tables("Pupils").Rows(i).Item("Oponent_Y").ToString <> ds_G.Tables("Pupils_1").Rows(j).Item("Forename_G") Then
  76.  
  77. ds_G.Tables("Pupils_1").Rows(j).Item(g) = True
  78. ds_Y.Tables("Pupils").Rows(i).Item(g) = True
  79. ds_Y.Tables("Pupils").Rows(i).Item("Oponent_Y") = ds_G.Tables("Pupils_1").Rows(j).Item("Forename_G")
  80. ds_G.Tables("Pupils_1").Rows(j).Item("Oponent_G") = ds_Y.Tables("Pupils").Rows(i).Item("Forename_Y")
  81.  
  82.  
  83. Dim cb_G As New OleDb.OleDbCommandBuilder(da_g)
  84. Dim updateCommand As New OleDbCommand("UPDATE abc SET Forename_G = ?, Class_G = ?", con)
  85.  
  86. updateCommand.Parameters.Add("Forename_G", OleDbType.VarChar, 0, "Forename_G")
  87. updateCommand.Parameters.Add("Class_G", OleDbType.VarChar, 0, "Forename_G")
  88.  
  89.  
  90. da_g.UpdateCommand = updateCommand
  91.  
  92.  
  93. da_t.Update(dt_G)
  94. da_t.Update(dt_T, "tmpFixtures") 'error
  95.  
  96. updateCommand.Parameters.Add("c", OleDbType.Integer, 0, "c")
  97. updateCommand.Parameters.Add("c", OleDbType.VarChar, 0, "c")
  98.  
  99. da_g.Update(ds_Y, "pupils")
  100.  
  101.  
  102. Exit For
  103. End If
  104. End If
  105. End If
  106.  
  107. Next j
  108. Exit For
  109. End If
  110.  
  111. Next i
  112. Exit For
  113. Next g
  114.  
  115. con.Close()
  116. End Sub

Thanks

Ryan
Last edited by Mr Brownstone; Mar 13th, 2008 at 9:33 am. Reason: bad grammar :)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 169
Reputation: ptaylor965 is an unknown quantity at this point 
Solved Threads: 19
Sponsor
ptaylor965's Avatar
ptaylor965 ptaylor965 is offline Offline
Junior Poster

Re: How can I update my access database via VB.net using this code

 
0
  #2
Mar 13th, 2008
Hi Try Using


Insert & Update command
  1. Dim OLECon As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = db.mdb")
  2. Dim OLECmd As New OleDb.OleDbCommand
  3. Dim OLEStr as String
  4.  
  5. OLEStr ="UPDATE abc SET"
  6. OLEStr += " Forname_G = '" & ds_G.Tables("Pupils_1").Rows(j).Item("Forename_G") & "',"
  7. OLEStr += " Class_G = '" & ds_G.Tables("Pupils_1").Rows(j).Item("Class_G") & "'"
  8.  
  9. OLECmd = New OleDb.OleDbCommand(OLEStr, OLEConn)
  10.  
  11. OLECon.Open()
  12. OLECmd.ExecuteNonQuery()
  13. OLECon.Close()

It works for SQL and i believe it also works for access databases
Peter Taylor
Visual Basic.NET Application Developer

TaylorsNet
http://www.taylorsnet.co.uk
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: Mr Brownstone is an unknown quantity at this point 
Solved Threads: 1
Mr Brownstone's Avatar
Mr Brownstone Mr Brownstone is offline Offline
Newbie Poster

Re: How can I update my access database via VB.net using this code

 
0
  #3
Mar 14th, 2008
Thanks alot it Worked
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC