943,648 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3635
  • VB.NET RSS
Jan 20th, 2009
0

Problems with executeNonQuery and connection

Expand Post »
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:
VB.NET Syntax (Toggle Plain Text)
  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():
VB.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ireneotom is offline Offline
2 posts
since Jan 2009
Jan 20th, 2009
0

Re: Problems with executeNonQuery and connection

Wrong forum
Featured Poster
Reputation Points: 1448
Solved Threads: 21
Posting Maven
GrimJack is offline Offline
2,735 posts
since Feb 2004
Jan 21st, 2009
0

Re: Problems with executeNonQuery and connection

Is prcConnect called before cmdSave_Click ? If so, close the connection
VB.NET Syntax (Toggle Plain Text)
  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
VB.NET Syntax (Toggle Plain Text)
  1. Try
  2. Con.Open()
  3. cmd.ExecuteNonQuery()
  4. Con.Close()
  5. ' Success
  6. Catch ex As Exception
  7. ' Handle error
  8. End try
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Feb 4th, 2009
0

Re: Problems with executeNonQuery and connection

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ireneotom is offline Offline
2 posts
since Jan 2009
Feb 4th, 2009
0

Re: Problems with executeNonQuery and connection

First, try it in the same sub/function. If it works you can export the functionality to some global functions/subs

try this:
VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 33
Solved Threads: 10
Junior Poster in Training
4advanced is offline Offline
67 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Add Dynmaic Data in DataGrid
Next Thread in VB.NET Forum Timeline: .Net Function & API





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC