oledb error

shingo99

New member
Joined
Dec 16, 2004
Messages
3
Programming Experience
Beginner
hello
i have build a database using MS ACCESS and write a coding in VB.NET to connect it as below
VB.NET:
Imports System.Data.OleDb

Public Class addItem
	Inherits System.Windows.Forms.Form
	Dim myconnection As New OleDbConnection("provider=microsoft.jet.oledb.4.0; datasource = Application.ExecutablePath + \LibrarySystem.mdb")

Private Sub saveB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveB.Click
		myconnection.Open()
		Dim mycom As New OleDbCommand("INSERT INTO Product(product_code) VALUES ('" & productTb.Text & "')", myconnection)
		Dim MyReader As OleDbDataReader = mycom.ExecuteReader()
		mycom.ExecuteNonQuery()
		myconnection.Close()
		MyReader.Close()
		mycom.Dispose()
	End Sub

but when i click on the saveB button, the error below show up
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

and this error points to the code below
VB.NET:
 myconnection.Open()

may i know what i did wrong?
thank you in advance
 
You need to concatenate the connection string much like you did the command text for the OleDbCommand. Also use Application.StartupPath instead of Application.ExecutablePath because the latter returns the full path of the executable including the filename. Change it to this:
VB.NET:
Dim myconnection As New OleDbConnection("provider=microsoft.jet.oledb.4.0; datasource=" & _
  Application.StartupPath & "\LibrarySystem.mdb")
You should also remove the line 'Dim MyReader As OleDbDataReader = mycom.ExecuteReader()' because you also have 'mycom.ExecuteNonQuery()' which is sufficient for an insert statement. There is no need to use a reader to execute an Insert command since you're not retrieving information.
 
Back
Top