im trying to get the insert to work but the problem i am facing is i keep getting this error

Can someone help please

thanks in advance my code is

Dim strsql As String = "Insert into cg_security_user (user_name,user_pwd) values (@field1,@field2) "

       Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand

       With objcmd
           .Parameters.AddWithValue("@field1", txtuser.Text)
           .Parameters.AddWithValue("@field2", txtpass.Text)
        
       End With
       objcmd.ExecuteNonQuery()
       objcmd.Dispose()
       MsgBox("Saved")

Recommended Answers

All 6 Replies

Because u declare acscmd as OleDb.OleDbCommand Object. But use objcmd in your code. Actually objcmd is not declared So you get the error.

Imports System.Data.OleDb


Public Class Form2
    Dim objcmd As OleDbCommand
    
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DataSet.cg_security_user' table. You can move, or remove it, as needed.
        Me.Cg_security_userTableAdapter.Fill(Me.DataSet.cg_security_user)
        'TODO: This line of code loads data into the 'DataSet.cg_security_role' table. You can move, or remove it, as needed.
        Me.Cg_security_roleTableAdapter.Fill(Me.DataSet.cg_security_role)

    End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connect()
    End Sub
    Sub connect()
        acsconn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\HP Sajad\Documents\Visual Studio 2010\Projects\login1\login1\security.mdb"
        acsconn.Open()
        If acsconn.State = ConnectionState.Open Then
            MsgBox("Connected")

        End If
        acsconn.Close()


        Dim strsql As String = "Insert into cg_security_user (user_name,user_pwd) values (@field1,@field2) "

        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand

        With objcmd
            .Parameters.AddWithValue("@field1", txtuser.Text)
            .Parameters.AddWithValue("@field2", txtpass.Text)
         
        End With
        objcmd.ExecuteNonQuery()
        objcmd.Dispose()
        MsgBox("Saved")

    


    End Sub




  
    Private Sub txtcreatdate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class

this is full script it has been declared on the top

Too much error ..

Imports System.Data.OleDb


Public Class Form2
    Dim objcmd As OleDbCommand' You create a reference not an object. You have to declare like Dim  objcmd As New OleDbCommand. Use New Keyword for that
    
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DataSet.cg_security_user' table. You can move, or remove it, as needed.
        Me.Cg_security_userTableAdapter.Fill(Me.DataSet.cg_security_user)
        'TODO: This line of code loads data into the 'DataSet.cg_security_role' table. You can move, or remove it, as needed.
        Me.Cg_security_roleTableAdapter.Fill(Me.DataSet.cg_security_role)

    End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connect()
    End Sub
    Sub connect()
        acsconn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\HP Sajad\Documents\Visual Studio 2010\Projects\login1\login1\security.mdb"
        acsconn.Open()
        If acsconn.State = ConnectionState.Open Then
            MsgBox("Connected")

        End If
        acsconn.Close()


        Dim strsql As String = "Insert into cg_security_user (user_name,user_pwd) values (@field1,@field2) "'Here you declare the String but never assigned it to any command. Why???

        Dim acscmd As New OleDb.OleDbCommand ' Here you create an object of OleDb.OleDbCommand but where you use this??

        With objcmd ' You assign the Parameter but where you assign the command String??
            .Parameters.AddWithValue("@field1", txtuser.Text)
            .Parameters.AddWithValue("@field2", txtpass.Text)
         
        End With
        objcmd.ExecuteNonQuery()
        objcmd.Dispose()
        MsgBox("Saved")

    


    End Sub




  
    Private Sub txtcreatdate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class

Now the clear code

Imports System.Data.OleDb


Public Class Form2
    Dim objcmd As OleDbCommand
    
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DataSet.cg_security_user' table. You can move, or remove it, as needed.
        Me.Cg_security_userTableAdapter.Fill(Me.DataSet.cg_security_user)
        'TODO: This line of code loads data into the 'DataSet.cg_security_role' table. You can move, or remove it, as needed.
        Me.Cg_security_roleTableAdapter.Fill(Me.DataSet.cg_security_role)

    End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connect()
    End Sub
    Sub connect()
        acsconn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\HP Sajad\Documents\Visual Studio 2010\Projects\login1\login1\security.mdb"
        acsconn.Open()
        If acsconn.State = ConnectionState.Open Then
            MsgBox("Connected")

        End If
        acsconn.Close()


        Dim strsql As String = "Insert into cg_security_user (user_name,user_pwd) values (@field1,@field2) "

       objcmd= New OleDb.OleDbCommand ' the oledbcommand
objcmd.CommandString=strsql 
        With objcmd
            .Parameters.AddWithValue("@field1", txtuser.Text)
            .Parameters.AddWithValue("@field2", txtpass.Text)
         
        End With
        objcmd.ExecuteNonQuery()
        objcmd.Dispose()
        MsgBox("Saved")
    End Sub

End Class

Now try this and see for any error.

hello !

'please use new in this line 
 Dim objcmd As OleDbCommand
'----------------------------
'like this 
 Dim objcmd As new OleDbCommand

this will solve your prob .

Regards

@wakasas..... Please see in my code i already use New into Sub Button1_Click event....

well @king , there is not that much time i have to read code , i just saw the question , and i reply it , but as you said you also use new key word , but if you use simple way of helping and just keep your code to the point , then it is time saving and easy to understand ,as you write full code twice , that is not imp.

Stay blessed

M.Waqas Aslam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.