Problems with executeNonQuery and connection

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 2
Reputation: ireneotom is an unknown quantity at this point 
Solved Threads: 0
ireneotom ireneotom is offline Offline
Newbie Poster

Problems with executeNonQuery and connection

 
0
  #1
Jan 20th, 2009
Hello Friends please i am in trouble with ExecuteNonQuery() and my connection and i need your help. I have been trying to build a small program and seem to have been lost as to how to solve my problem.

The exception details are
Exception Details:InvalidOperationException was Unhandled
ExecuteNonQuery: Connection property has not been initialized

The Source of error is
cmd.ExecuteNonQuery()

and my relevant code is shown below

For Connection:
  1. Private Sub FrmStudent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. OleDbConnection1.ConnectionString = strCon
  3. Refresh_Form()
  4.  
  5. End Sub
  6.  
  7. Module ModuleConnection
  8. Public Con As System.Data.OleDb.OleDbConnection
  9. Public U, P, S, D, strCon As String
  10. Public ObjCon As FrmConnection
  11. Public Sub prcConnect(ByVal U, ByVal P, ByVal S, ByVal D)
  12. Try
  13. strCon = "provider=SQLOLEDB;User id=" & U & ";Password=" & P & ";Server=" & S & ";database=" & D
  14. Con = New System.Data.OleDb.OleDbConnection(strCon)
  15. Con.Open()
  16. MsgBox("Connection to the Database is Successful", MsgBoxStyle.Information, "Welcome")
  17. Catch ex As Exception
  18. MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error Connecting To Database")
  19. ObjCon = New FrmConnection
  20. ObjCon.Show()
  21. End Try
  22. End Sub
  23.  
  24. End Module
For ExecuteNonQuery():
  1. Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
  2.  
  3. If Len(Trim(txtstudCd.Text)) = 0 Then
  4. MessageBox.Show("Please Student Id cannot be blank", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  5. Exit Sub
  6. End If
  7. If Len(Trim(txtFirstName.Text)) = 0 Then
  8. MsgBox("Please FirstName cannot be blank", MsgBoxStyle.OkOnly, "Enter Name")
  9. Exit Sub
  10. End If
  11. If Len(Trim(txtLastName.Text)) = 0 Then
  12. MsgBox("Please LastName cannot be blank", MsgBoxStyle.OkOnly, "Enter Name")
  13. Exit Sub
  14. End If
  15.  
  16. If Not IsNumeric(txtAge.Text) Then
  17. MessageBox.Show("Please Age must be Numeric value", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  18. txtAge.Text = ""
  19. txtAge.Focus()
  20. Exit Sub
  21. End If
  22.  
  23. If Not IsNumeric(txtstudCd.Text) Then
  24. MessageBox.Show("Please Student Id must be Numeric Value", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  25. txtstudCd.Text = ""
  26. txtstudCd.Focus()
  27.  
  28. End If
  29.  
  30.  
  31. Dim Con As New OleDb.OleDbConnection(strCon)
  32. Dim strSQL As String = _
  33. "INSERT INTO Student (StudentCode, FirstName, LastName, Age)" & _
  34. " VALUES(@StudentCode, @FirstName, @LastName, @Age )"
  35.  
  36. Dim cmd As New OleDb.OleDbCommand(strCon)
  37. With cmd
  38. .Parameters.Add(New OleDb.OleDbParameter("@StudentCode", SqlDbType.NVarChar, 6)).Value = txtstudCd.Text
  39. .Parameters.Add(New OleDb.OleDbParameter("@FirstName", SqlDbType.NVarChar, 30)).Value = txtFirstName.Text
  40. .Parameters.Add(New OleDb.OleDbParameter("@LastName", SqlDbType.NVarChar, 30)).Value = txtLastName.Text
  41. .Parameters.Add(New OleDb.OleDbParameter("@Age", SqlDbType.NVarChar, 3)).Value = txtAge.Text
  42. End With
  43. Con.Open()
  44. cmd.ExecuteNonQuery()
  45. Con.Close()
  46. MsgBox("Data Saved", MsgBoxStyle.Information, "Hello")
  47. Refresh_Form()
  48.  
  49. End Sub
Last edited by Ancient Dragon; Jan 20th, 2009 at 11:03 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 2,165
Reputation: GrimJack will become famous soon enough GrimJack will become famous soon enough 
Solved Threads: 15
Featured Poster
GrimJack's Avatar
GrimJack GrimJack is offline Offline
Postaholic

Re: Problems with executeNonQuery and connection

 
0
  #2
Jan 20th, 2009
Wrong forum
Imagine a world without hypotheticals....
The problem with your gene pool is that there’s no lifeguard.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Problems with executeNonQuery and connection

 
0
  #3
Jan 21st, 2009
Is prcConnect called before cmdSave_Click ? If so, close the connection
  1. Try
  2. strCon = "provider=SQLOLEDB;User id=" & U & ";Password=" & P & ";Server=" & S & ";database=" & D
  3. Con = New System.Data.OleDb.OleDbConnection(strCon)
  4. Con.Open()
  5. MsgBox("Connection to the Database is Successful", MsgBoxStyle.Information, "Welcome")
  6. Catch ex As Exception
  7. MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error Connecting To Database")
  8. ObjCon = New FrmConnection
  9. ObjCon.Show()
  10. Finally
  11. If Con IsNot Nothing AndAlso Con.State = ConnectionState.Open Then
  12. Con.Close()
  13. End If
  14. End Try
Put a breakpoint in cmdSave_Click to line Dim Con As New OleDb.OleDbConnection(strCon) and check that you actually have a valid connection string.

Add a connection object to the command object Dim cmd As New OleDb.OleDbCommand(strSQL, Con) .

Finally, put your DB code inside Try...Catch block
  1. Try
  2. Con.Open()
  3. cmd.ExecuteNonQuery()
  4. Con.Close()
  5. ' Success
  6. Catch ex As Exception
  7. ' Handle error
  8. End try
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: ireneotom is an unknown quantity at this point 
Solved Threads: 0
ireneotom ireneotom is offline Offline
Newbie Poster

Re: Problems with executeNonQuery and connection

 
0
  #4
Feb 4th, 2009
Hello Friends,
i am back again Please my code is still not working.
i placed a breakpoint to my connection string and added a connection object to the command object and i put the DB code inside a Try...Catch Block. as Team64 suggested.
It is not gennerating any error but when i preview the data through the dataadapter it does not indicated that the data is in the database.
I am counting on your contributions

Thanks

ireneotom
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: 4advanced is an unknown quantity at this point 
Solved Threads: 10
4advanced 4advanced is offline Offline
Junior Poster in Training

Re: Problems with executeNonQuery and connection

 
0
  #5
Feb 4th, 2009
First, try it in the same sub/function. If it works you can export the functionality to some global functions/subs

try this:
  1. Try
  2. strCon = "provider=SQLOLEDB;User id=" & U & ";Password=" & P & ";Server=" & S & ";database=" & D
  3. Con = New System.Data.OleDb.OleDbConnection(strCon)
  4. Con.Open()
  5.  
  6. 'EXECUTE YOUR COMMAND HERE
  7. Dim strSQL As String = _
  8. "INSERT INTO Student (StudentCode, FirstName, LastName, Age)" & _
  9. " VALUES(@StudentCode, @FirstName, @LastName, @Age )"
  10.  
  11. Dim cmd As New OleDb.OleDbCommand(Con)
  12. With cmd
  13. .Parameters.AddWithValue("@StudentCode", txtstudCd.Text)
  14. .Parameters.AddWithValue("@FirstName", txtFirstName.Text)
  15. .Parameters.AddWithValue("@LastName", txtLastName.Text)
  16. .Parameters.AddWithValue("@Age" txtAge.Text)
  17. End With
  18. cmd.ExecuteNonQuery()
  19.  
  20.  
  21. Catch ex As Exception
  22. MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error Connecting To Database")
  23.  
  24. Finally
  25. If Con IsNot Nothing AndAlso Con.State = ConnectionState.Open Then
  26. Con.Close()
  27. 'cleanup
  28. Con.Dispose
  29. Con = Nothing
  30. cmd = nothing
  31. End If
  32. End Try
Last edited by 4advanced; Feb 4th, 2009 at 9:47 am. Reason: cleanup code added
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC