insert into MS Access using vb.NEt

nsj_1983

New member
Joined
Dec 23, 2004
Messages
2
Programming Experience
Beginner
My code is

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim strConn As String

Dim strSQL As String

Dim Conn As OleDb.OleDbConnection

Dim cmd As OleDb.OleDbCommand

Label1.Visible =
False

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=C:\db2.mdb;" & _

"User ID=Admin;" & _

"Password="





'Pass the Connection string to OleDbConnection object.

Conn = New OleDb.OleDbConnection(strConn)

cmd =
New OleDb.OleDbCommand(strSQL, Conn)

Conn.Open()

'Build the SQL strings.

strSQL = "insert into table1 values( 99,'sad')"

Try

cmd.ExecuteNonQuery()

Catch ex As OleDb.OleDbException

Label1.Visible =
True

Label1.Text = ex.ToString

End Try

Conn.Close()

End Sub





The database table is table1 in db1.mdb.

the error occurs at cmd.ExecuteNonQuery()

could anyone help me . please!
 
It did not help me. it still get the following error:

System.Data.OleDb.OleDbException: Command text was not set for the command object. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at WebApplication1.insertrec.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\WebApplication1\insertrec.aspx.vb:line 56

System.Data.OleDb.OleDbException: Command text was not set for the command object. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at WebApplication1.insertrec.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\WebApplication1\insertrec.aspx.vb:line 56 System.Data.OleDb.OleDbException: Command text was not set for the command object. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at WebApplication1.insertrec.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\WebApplication1\insertrec.aspx.vb:line 56
System.Data.OleDb.OleDbException: Command text was not set for the command object. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at WebApplication1.insertrec.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\WebApplication1\insertrec.aspx.vb:line 56
 
oh ic the problem, move the command string to before the 'cmd = New OleDb.OleDbCommand(strSQL, Conn)'
 
Back
Top