2 Replies - 3656 Views - Last Post: 27 January 2012 - 02:30 AM Rate Topic: ****- 1 Votes

#1 milind7007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-January 12

ExecuteNonQuery: Connection property has not been initialized

Posted 27 January 2012 - 01:56 AM

When i am try to execute following code it gives "ExecuteNonQuery: Connection property has not been initialized" error,
several time i tried to resolve this problem, but couldn't solve it. Please assist me.


Try
           Dim cmd As New OleDbCommand
           Mycn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\MILIND\Structure\Reports_DB.mdb;")
           Mycn.Open()

           SQLstr = "INSERT INTO Collection_Rpt_Tbl VALUES('" & CR.micro_id & "','" & CR.comp_name & "','" & CR.date1 & "','" & CR.premium & "')"

           Command = New OleDbCommand(SQLstr, Mycn)

           icount = cmd.ExecuteNonQuery()
           MessageBox.Show(icount)

       Catch ex As Exception
           MessageBox.Show(ex.Message)
           Mycn.Close()
       End Try

This post has been edited by CodingSup3rnatur@l-360: 27 January 2012 - 02:04 AM
Reason for edit:: Please use code tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: ExecuteNonQuery: Connection property has not been initialized

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: ExecuteNonQuery: Connection property has not been initialized

Posted 27 January 2012 - 02:14 AM

Hi,

Please use code tags when posting code, as so:

:code:

Also, I have moved this to the VB.NET forum for you.


As for your problem, you are passing in the Mycn variable to the OleDbCommand object that you reference in the variable named Command. That's good. However, you don't pass Mycn in to the other OleDbCommand object you have created and are referencing via the cmd variable. Unfortunately for you, the cmd variable is the one you are calling ExecuteNonQuery() on, and as it has no connection object associated with it, you get this error.

I'm guessing you probably wanted this:

icount = Command.ExecuteNonQuery()


instead of this:

icount = cmd.ExecuteNonQuery()

This post has been edited by CodingSup3rnatur@l-360: 27 January 2012 - 02:17 AM

Was This Post Helpful? 2
  • +
  • -

#3 milind7007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-January 12

Re: ExecuteNonQuery: Connection property has not been initialized

Posted 27 January 2012 - 02:30 AM

View PostCodingSup3rnatur@l-360, on 27 January 2012 - 02:14 AM, said:

Hi,

Please use code tags when posting code, as so:

:code:

Also, I have moved this to the VB.NET forum for you.


As for your problem, you are passing in the Mycn variable to the OleDbCommand object that you reference in the variable named Command. That's good. However, you don't pass Mycn in to the other OleDbCommand object you have created and are referencing via the cmd variable. Unfortunately for you, the cmd variable is the one you are calling ExecuteNonQuery() on, and as it has no connection object associated with it, you get this error.

I'm guessing you probably wanted this:

icount = Command.ExecuteNonQuery()


instead of this:

icount = cmd.ExecuteNonQuery()


thanks a lot, you provide good info it will worth a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1