none
ExecuteNonQuery: Connection property has not been initialized. RRS feed

  • Question

  • Hi i'm trying to pass Datagrid Data to Ms Access. while i'm trying i got above Titled Error. please help me about this.

    here is my code.

    Dim dg As New OleDbDataAdapter("select * from Master", cn)
            For m As Integer = 0 To dgv1.Rows.Count - 1
                dg.InsertCommand = New OleDbCommand("insert into Master values(  '" & dgv1.Item(0, m).Value & "','" & dgv1.Item(1, m).Value & "','" & dgv1.Item(2, m).Value & "')")

                cn.Open()
                dg.InsertCommand.ExecuteNonQuery()

                cn.Close()
            Next m

    Wednesday, July 18, 2012 4:20 PM

Answers

  • You should pass a connection object to your OleDbCommand constructor i.e.

    dg.InsertCommand = new OleDbCommand(<your Insert Query>, cn)

    OR

    Assign a connection object to your insert command after you initialize oledb command.

    dg.InsertCommand.Connection=cn


    Lingaraj Mishra

    Thursday, July 19, 2012 9:01 AM

All replies

  • Make sure you assigned connection to Connection property of your OleDbCommand if you want to call ExecuteNonQuery method directly from your command. You actually do not need to use OleDbDataAdapter at all in your case, just create OleDbCommand directly.


    Val Mazur Microsoft MVP

    http://www.xporttools.net


    Wednesday, July 18, 2012 4:33 PM
    Moderator
  • hi

    i'm just starter for VB.net. please help me about this.

    thanks

    Wednesday, July 18, 2012 4:43 PM
  • You should pass a connection object to your OleDbCommand constructor i.e.

    dg.InsertCommand = new OleDbCommand(<your Insert Query>, cn)

    OR

    Assign a connection object to your insert command after you initialize oledb command.

    dg.InsertCommand.Connection=cn


    Lingaraj Mishra

    Thursday, July 19, 2012 9:01 AM