help badly needed.......newbie

bzeus

Member
Joined
Feb 22, 2005
Messages
14
Programming Experience
Beginner
I'm trying to connect to an access database using this coding:

Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim str AsString
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\eoaoffice\eoa.mdb;")

cn.Open()
str = "insert into customer(cus_namess) values('" & names.Text & "')"
cmd = New OleDbCommand(Str, cn)
cmd.ExecuteScalar()
cn.Close()

When the code is ran this error shows:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll for the line that shows cmd.ExecuteScalar()...

please help me....thank you so much....

 

bonn

New member
Joined
Mar 1, 2005
Messages
2
Programming Experience
3-5
try using

cmd.ExecuteNonQuery()
 

bzeus

Member
Joined
Feb 22, 2005
Messages
14
Programming Experience
Beginner
i'm getting the same error for the same line.....
 

shelly121

Member
Joined
Feb 17, 2005
Messages
15
Programming Experience
Beginner
bzeus said:
i'm getting the same error for the same line.....
this is how i did mine:
Imports System.IO
PublicClass Form1
inherits System.Windows.Forms.Form
im CN AsNew ADODB.Connection()
Dim RS AsNew ADODB.Recordset()
Region " Windows Form Designer generated code "

PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
CN =
New ADODB.Connection()
CN.Provider = "MICROSOFT.JET.OLEDB.4.0" 'ACCESS CONNECTION
CN.Open("a:\ something\mortgage.MDB")
RS =
New ADODB.Recordset()
RS.Open("select *from client order by Name", CN, 1, 2)

 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
I wouldn't suggest using the ADODB as shelly121 has shown as the is the older way of doing it.
The way you have shown (using the new ADO.NET) should have worked as I don't see any mistakes.
You could place Try/Catch blocks to catch and display the error message.

How did you solve the problem?
 

bzeus

Member
Joined
Feb 22, 2005
Messages
14
Programming Experience
Beginner
i created the same database structure in sql server and use a sql server connection it worked....but i still don't know what was wrong with the previous coding done with access database.......:)
 

DanGoodhew86

New member
Joined
Nov 17, 2005
Messages
3
Programming Experience
3-5
your original code... only had one error. You want to open a command not a database connection... leave the DbConnection closed until you execute the query

If you were to change the line cn.open to cmd.connection.open your code would be fine.
 
Top Bottom