Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 Error trying to add record to Access 2003 Database with DataAdapter

$
0
0
My program reads the values in the table to a bound combobox. The user can either select from the combobox, in which case the "Update" button is enabled or freetype in the combobox, in which case, the "Add" button is enabled.
The code to populate the comboboxes is as follows
vb.net Code:
  1. Public Class frmMain
  2.     Public MoveLeft As Boolean
  3.     Public Conn As New OleDb.OleDbConnection
  4.     Public dbProvider As String
  5.     Public dbSource As String
  6.     Public ds As New DataSet
  7.     Public daCategory As OleDb.OleDbDataAdapter
  8.     Public daLevel As OleDb.OleDbDataAdapter
  9.     Public SQL As String
  10.     Public Action As Integer
  11.         'populate "list by" combobox
  12.         cboListBy.Items.Clear()
  13.         cboListBy.Items.Add("Alphabetically")
  14.         cboListBy.Items.Add("Word category")
  15.         'set form icon
  16.         Me.Icon = My.Resources.UKFlag
  17.         'set panel to be off the right side of the form and therefore not visible. It can be scrolled in from there
  18.         pnlManagement.Left = 1047
  19.  
  20.         'set database stuff
  21.         dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0"
  22.         dbSource = "Data Source=" & Application.StartupPath & "\Vocab.mdb"
  23.         Conn.ConnectionString = dbProvider & ";" & dbSource
  24.         Conn.Open()
  25.         SQL = "SELECT * FROM tblCategory ORDER BY ctName"
  26.         daCategory = New OleDb.OleDbDataAdapter(SQL, Conn)
  27.         daCategory.Fill(ds, "tblCategory")
  28.         Dim bndCategory As New BindingSource
  29.         bndCategory.DataSource = ds.Tables("tblCategory")
  30.         cboCategoryMaint.DataSource = bndCategory
  31.         cboCategoryMaint.ValueMember = "ctID"
  32.         cboCategoryMaint.DisplayMember = "ctName"
  33.  
  34.         SQL = "SELECT * FROM tblLevel"
  35.         daLevel = New OleDb.OleDbDataAdapter(SQL, Conn)
  36.         daLevel.Fill(ds, "tblLevel")
  37.  
  38.         Dim bndLevel As New BindingSource
  39.         bndLevel.DataSource = ds.Tables("tblLevel")
  40.         cboLevelMaint.DataSource = bndLevel
  41.         cboLevelMaint.ValueMember = "lvID"
  42.         cboLevelMaint.DisplayMember = "lvName"
  43.         Conn.Close()
  44.         Conn.Dispose()
  45.         cboLevelMaint.SelectedIndex = -1
  46.         cboCategoryMaint.SelectedIndex = -1
  47.     End Sub

This works fine. The next part causes the error. When a user clicks the Add button, the following code runs and execution stops at line 7 "da.Fill(ds,"tblLevel") with the error message "You haven't initialised the connectionstring property".
vb.net Code:
  1. Private Sub btnLevelAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnLevelAdd.Click
  2.         Dim SQL As String = "SELECT * FROM tblLevel"
  3.         Dim insertSql As String = "INSERT INTO tblLevel (lvName) VALUE(@nm)"
  4.         Dim da As New OleDb.OleDbDataAdapter
  5.         da.SelectCommand = New OleDb.OleDbCommand(SQL, Conn)
  6.         Dim ds As New DataSet
  7.         da.Fill(ds, "tblLevel")
  8.         Dim dt As DataTable = ds.Tables("tblLevel")
  9.         Dim newRow As DataRow = dt.NewRow()
  10.         newRow("lvName") = cboLevelMaint.Text
  11.         dt.Rows.Add(newRow)
  12.         Dim insertCmd As New OleDb.OleDbCommand(insertSql, Conn)
  13.         insertCmd.Parameters.Add("@nm", CType(SqlDbType.NVarChar, OleDb.OleDbType), 10, "lvName")
  14.         da.InsertCommand = insertCmd
  15.         da.Update(ds, "tblName")
  16.         ShowAdded()
  17.     End Sub

I have to admit I'm all at sea with these new things DataTable DataSet etc in .net and am struggling to understand it. The code I have above was ripped from another post on this forum. In that post JMc mentioned that da.Fill was not necessary and that da.FillSchema was more appropriate because we are only adding a new row. However when I change it to "da.FillSchema(ds,"tblLevel"), the IDE complains with an error message "Overload resolution failed because no accessible 'FillSchema' can be called with these arguments.

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>