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
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".
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.
The code to populate the comboboxes is as follows
vb.net Code:
Public Class frmMain Public MoveLeft As Boolean Public Conn As New OleDb.OleDbConnection Public dbProvider As String Public dbSource As String Public ds As New DataSet Public daCategory As OleDb.OleDbDataAdapter Public daLevel As OleDb.OleDbDataAdapter Public SQL As String Public Action As Integer 'populate "list by" combobox cboListBy.Items.Clear() cboListBy.Items.Add("Alphabetically") cboListBy.Items.Add("Word category") 'set form icon Me.Icon = My.Resources.UKFlag 'set panel to be off the right side of the form and therefore not visible. It can be scrolled in from there pnlManagement.Left = 1047 'set database stuff dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0" dbSource = "Data Source=" & Application.StartupPath & "\Vocab.mdb" Conn.ConnectionString = dbProvider & ";" & dbSource Conn.Open() SQL = "SELECT * FROM tblCategory ORDER BY ctName" daCategory = New OleDb.OleDbDataAdapter(SQL, Conn) daCategory.Fill(ds, "tblCategory") Dim bndCategory As New BindingSource bndCategory.DataSource = ds.Tables("tblCategory") cboCategoryMaint.DataSource = bndCategory cboCategoryMaint.ValueMember = "ctID" cboCategoryMaint.DisplayMember = "ctName" SQL = "SELECT * FROM tblLevel" daLevel = New OleDb.OleDbDataAdapter(SQL, Conn) daLevel.Fill(ds, "tblLevel") Dim bndLevel As New BindingSource bndLevel.DataSource = ds.Tables("tblLevel") cboLevelMaint.DataSource = bndLevel cboLevelMaint.ValueMember = "lvID" cboLevelMaint.DisplayMember = "lvName" Conn.Close() Conn.Dispose() cboLevelMaint.SelectedIndex = -1 cboCategoryMaint.SelectedIndex = -1 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:
Private Sub btnLevelAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnLevelAdd.Click Dim SQL As String = "SELECT * FROM tblLevel" Dim insertSql As String = "INSERT INTO tblLevel (lvName) VALUE(@nm)" Dim da As New OleDb.OleDbDataAdapter da.SelectCommand = New OleDb.OleDbCommand(SQL, Conn) Dim ds As New DataSet da.Fill(ds, "tblLevel") Dim dt As DataTable = ds.Tables("tblLevel") Dim newRow As DataRow = dt.NewRow() newRow("lvName") = cboLevelMaint.Text dt.Rows.Add(newRow) Dim insertCmd As New OleDb.OleDbCommand(insertSql, Conn) insertCmd.Parameters.Add("@nm", CType(SqlDbType.NVarChar, OleDb.OleDbType), 10, "lvName") da.InsertCommand = insertCmd da.Update(ds, "tblName") ShowAdded() 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.