5 Replies - 312 Views - Last Post: 29 November 2012 - 10:05 PM Rate Topic: -----

#1 adhikarysaurav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-November 12

problem with the follwing code

Posted 28 November 2012 - 04:00 AM

Dim cnn As OleDbConnection

Dim cmdInsertStock As New OleDbCommand

cnn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & Application.StartupPath & "\Database.mdb")
Dim insStock As OleDbCommand = New OleDbCommand("Insert into Stock values ('" & txtModelNo.Text & "','" & txtDescription.Text & "','" & txtColour.Text & "'cnn)")


cnn.Open()
cmdInsertStock.ExecuteNonQuery()
cnn.Close()

error showing.....

executenonquery connection property has not been initialized

Saurav

Is This A Good Question/Topic? 0
  • +

Replies To: problem with the follwing code

#2 Swapnilbarde007  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 20-September 12

Re: problem with the follwing code

Posted 28 November 2012 - 06:01 AM

Hi, please check

OleDbCommand("Insert into Stock values ('" & txtModelNo.Text & "','" & txtDescription.Text & "','" & txtColour.Text & "'cnn)")

you forgot ","

Must be

OleDbCommand("Insert into Stock values ('" & txtModelNo.Text & "','" & txtDescription.Text & "','" & txtColour.Text & "',cnn)")


Hope was useful.....
Was This Post Helpful? 0
  • +
  • -

#3 zeeshanef  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 70
  • Joined: 14-April 12

Re: problem with the follwing code

Posted 28 November 2012 - 06:03 AM

you did also join cnn with query, place comma(,) at the end of your query in order to use the OleDbCommand constructor overload.

Dim insStock As OleDbCommand = New OleDbCommand("Insert into Stock values ('" & txtModelNo & "','" & txtDescription & "','" & txtColour & "')", cnn)

This post has been edited by zeeshanef: 28 November 2012 - 08:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 adhikarysaurav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-November 12

Re: problem with the follwing code

Posted 28 November 2012 - 09:58 PM

View Postzeeshanef, on 28 November 2012 - 06:03 AM, said:

you did also join cnn with query, place comma(,) at the end of your query in order to use the OleDbCommand constructor overload.

Dim insStock As OleDbCommand = New OleDbCommand("Insert into Stock values ('" & txtModelNo & "','" & txtDescription & "','" & txtColour & "')", cnn)



thanks zeeshanef...
but problem is same...can't find out the mistake...anyway...traying...to overcome...

Saurav

thanks zeeshanef...
but problem is same...can't find out the mistake...anyway...traying...to overcome...

Saurav
Was This Post Helpful? 0
  • +
  • -

#5 zeeshanef  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 70
  • Joined: 14-April 12

Re: problem with the follwing code

Posted 29 November 2012 - 03:41 AM

It is because you initialize two different command objects. There is no use of cmdInsertStock because, you are creating and initializing new command object after this line.

After cnn.Open() put put this statement:
insStock.ExecuteNonQuery() and remove this cmdInsertStock.ExecuteNonQuery()

This should work:
 Dim cnn As OleDbConnection

 cnn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & Application.StartupPath & "\Database.mdb") 
 Dim insStock As OleDbCommand = New OleDbCommand("Insert into Stock values ('" & txtModelNo & "','" & txtDescription & "','" & txtColour & "')", cnn)

 cnn.Open()
 insStock.ExecuteNonQuery()
 cnn.Close()

This post has been edited by zeeshanef: 29 November 2012 - 03:44 AM

Was This Post Helpful? 0
  • +
  • -

#6 nilkanthnaik5  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 22-October 12

Re: problem with the follwing code

Posted 29 November 2012 - 10:05 PM

Hi adhikarysaurav

Use insStock.ExecuteNonQuery() instead of cmdInsertStock.ExecuteNonQuery()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1