0

im working on this piece of code and for sum reason i cannot understand why i keep getting this message can you please advise me

thanks in advance

'sql statements
        strsql = "insert into c_security_user (user_name, user_pwd,) values ('" & txtuser.Text & "','" & txtpass.Text & "')"
        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        cmd.ExecuteNonQuery() ' execute oledb commands non query only
        acscmd.Dispose()
        MsgBox("Saved")

the text in red is were the error shows

3
Contributors
26
Replies
27
Views
5 Years
Discussion Span
Last Post by kingsonprisonic
Featured Replies
  • In your post above where you said you still got the error you had renamed the command object to asccmd instead of acscmd. [code] Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand acscmd.CommandText = strsql 'sets the sql string acscmd.Connection = acsconn 'sets the connection to use the oledbcommand acscmd.ExecuteNonQuery() … Read More

0

its says

nullreferenceexception was unhandled.
object reference not set to an instance of an object

1

Maybe you have that cmd object declared elsewhere but from what you posted the name of the command object is wrong.

Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
 acscmd.CommandText = strsql 'sets the sql string
 acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
 cmd.ExecuteNonQuery() ' execute oledb commands non query only

You declare and set parameters for acscmd but then call the executeNonQuery on cmd.

Votes + Comments
+1 for eagle eye....
0

even when i change it to

Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
 acscmd.CommandText = strsql 'sets the sql string
 acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
 asccmd.ExecuteNonQuery() ' execute oledb commands non query only

i still get the same error message

0
Public acsda As New OleDbDataAdapter
 Public acsconn As New OleDbConnection
Public acsdr As OleDbDataReader
    Public strsql As String

    Sub connect()
        acsconn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=..mdb
        acsconn.Open()
        If acsconn.State = ConnectionState.Open Then
            MsgBox("Connected")
        End If
    End Sub
End Module




'sql statements
        strsql = "insert into c_security_user (user_name, user_pwd,) values ('" & txtuser.Text & "','" & txtpass.Text & "')"
        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        cmd.ExecuteNonQuery() ' execute oledb commands non query only
        acscmd.Dispose()
        MsgBox("Saved")

this is my full code if it helps please can some help me i been at it for over 2 hours

1

You do again wrong thing

Your code

Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
 acscmd.CommandText = strsql 'sets the sql string
 acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
' asccmd.ExecuteNonQuery() ' Here your problem Because acscmd is not asccmd

try

acscmd.ExecuteNonQuery()
Votes + Comments
Absolutely...
2

In your post above where you said you still got the error you had renamed the command object to asccmd instead of acscmd.

Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        acscmd.ExecuteNonQuery() ' execute oledb commands non query only
        acscmd.Dispose()

Copy that code and use it, you shouldn't get an error.

ARG!! beaten to it ;)

Edited by hericles: n/a

0

i just did that and now i got a different error

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

i have had a look and the connection is open

0

Try opening your connection before executing the query

Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand

acsconn.Open()'Open the connection first
        acscmd.ExecuteNonQuery() ' execute oledb commands non query only

acsconn.Close()
        acscmd.Dispose()
0

now it looks like dis

The ConnectionString property has not been initialized.

my code is here

strsql = "insert into c_security_user (user_name, user_pwd,) values ('" & txtuser.Text & "','" & txtpass.Text & "')"
        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        acsconn.Open() 'Open the connection first
        acscmd.ExecuteNonQuery() ' execute oledb commands non query only

        acsconn.Close()
        acscmd.Dispose()
2

Please notice carefully..

Sub connect()
        acsconn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=..mdb
        acsconn.Open()
        If acsconn.State = ConnectionState.Open Then
            MsgBox("Connected")
        End If
    End Sub

in your code

acsconn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=..mdb

It is not a complete connection string for your database...

First use correct connection string...

Goto : http://www.connectionstrings.com/

Try the correct Connection String Format...

0

when i debug the system there no erro but when i click go button it does not do anything

0
Imports System.Data.OleDb
Public Class Form2

    Dim cmd 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
    End Sub
    Sub connect()
        acsconn.ConnectionString = "[CODE][/CODE]..security.mdb"
        acsconn.Open()
        If acsconn.State = ConnectionState.Open Then
            MsgBox("Connected")
        End If



        strsql = "insert into c_security_user (user_name, user_pwd,) values ('" & txtuser.Text & "','" & txtpass.Text & "')"
        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        acsconn.Open() 'Open the connection first
        acscmd.ExecuteNonQuery() ' execute oledb commands non query only

        acsconn.Close()
        acscmd.Dispose()

    End Sub


End Class

Edited by Dani: Fixed formatting

0

You do not call connect() Subroutine anywhere on your code so nothing happen..

try to call it on button click event.

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

now this error has returned
Not allowed to change the 'ConnectionString' property. The connection's current state is open.

0

Ok OK OK.

Just replace your sub with this..

Sub connect()
        acsconn.ConnectionString = "..security.mdb"
        acsconn.Open()
        If acsconn.State = ConnectionState.Open Then
            MsgBox("Connected")
        End If
 
 acsconn.Close()
 
        strsql = "insert into c_security_user (user_name, user_pwd,) values ('" & txtuser.Text & "','" & txtpass.Text & "')"
        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        acsconn.Open() 'Open the connection first
        acscmd.ExecuteNonQuery() ' execute oledb commands non query only
 
        acsconn.Close()
        acscmd.Dispose()
 
    End Sub
0

im really sorry about this but again nothing seems to be happening

this is the code i have entered

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    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()

        strsql = "insert into c_security_user (user_name, user_pwd,) values ('" & txtuser.Text & "','" & txtpass.Text & "')"
        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        acsconn.Open() 'Open the connection first
        acscmd.ExecuteNonQuery() ' execute oledb commands non query only

        acsconn.Close()
        acscmd.Dispose()

    End Sub



End Class
0

Oh my god. Please first read the post clearly

U again miss the call for subroutine..

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()

        strsql = "insert into c_security_user (user_name, user_pwd,) values ('" & txtuser.Text & "','" & txtpass.Text & "')"
        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        acsconn.Open() 'Open the connection first
        acscmd.ExecuteNonQuery() ' execute oledb commands non query only

        acsconn.Close()
        acscmd.Dispose()

    End Sub



End Class

try this code.. And notice here in between the Button click event i use connect()..
so connect() get called...

0

that has beeen resolved but now this error has come up

Syntax error in INSERT INTO statement.
at

acscmd.ExecuteNonQuery() ' execute oledb commands non query only

its either my bad luck or this code just dont like me

0

Can you post up all of your code? Normally that error is accurate and so if it says the connection is closed it probably is.

Forget it... I didn't see there was this many pages. Ignore me:)

Edited by hericles: missed later posts

0

Try this

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()

        strsql = "insert into c_security_user (user_name, user_pwd) values ('" & txtuser.Text & "','" & txtpass.Text & "')"
        Dim acscmd As New OleDb.OleDbCommand ' the oledbcommand
        acscmd.CommandText = strsql 'sets the sql string
        acscmd.Connection = acsconn 'sets the connection to use the oledbcommand
        acsconn.Open() 'Open the connection first
        acscmd.ExecuteNonQuery() ' execute oledb commands non query only

        acsconn.Close()
        acscmd.Dispose()

    End Sub



End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.