Results 1 to 4 of 4

Thread: Using executeNonQuery to execute an INSERT stmt [RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    Savannah, GA
    Posts
    100

    Using executeNonQuery to execute an INSERT stmt [RESOLVED]

    I am attempting to use the executeNonQuery method of a
    command object to execute an insert SQL statement on a table in
    an Access database. I have successfully used executeNonQuery
    to perform an Update to a table but I can't seem to get this one
    to work. Here is my code, I have printed the generated query
    and tested it in access and the query works fine, must be
    something with my VB code. I'm new to .NET so I'm not seeing
    the problem. Any help is appreciated. Here is my code.

    VB Code:
    1. Private Sub Insert_Row()
    2.         Dim strSQL As String
    3.  
    4.         Dim strconn As String
    5.         Dim connection As New OleDb.OleDbConnection
    6.  
    7.         Dim a() As String = cmbExpenseAccount.SelectedItem.Split()
    8.  
    9.         strconn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = S:\20YRLongRangePlan\CapitalLogistics.mdb"
    10.  
    11.         strSQL = "INSERT INTO tblProjects VALUES(" & projectID
    12. & ", '', '', '" & a(0) & "', " & Chr(34) & txtDescription.Text & Chr(34)
    13. & ", " & Chr(34) & txtManufacturer.Text & Chr(34) & ", '', '', " & Chr
    14. (34) & txtProjectLife.Text & Chr(34) & ", " & Chr(34) &
    15. cmbReviewYear.SelectedItem & Chr(34) & ", " & Chr(34) &
    16. cmbReplacementYear.SelectedItem & Chr(34) & ", " & Chr(34) &
    17. txtArchitectComments.Text & Chr(34) & ", " & Chr(34) &
    18. cmbArchitect.SelectedItem & Chr(34) & ", '', '', '', '', " & Chr(34) &
    19. cmbGrant.SelectedItem & Chr(34) & ", '', '', '', '', '', '', '', '', " &
    20. Me.projDesc & ");"
    21.  
    22.  
    23.  
    24.         connection.ConnectionString = strconn
    25.         connection.Open()
    26.  
    27.  
    28.         Dim command As New OleDb.OleDbCommand
    29.  
    30.  
    31.         command.CommandText = strSQL
    32.  
    33.         command.Connection = connection
    34.  
    35.         command.ExecuteNonQuery()
    36.  
    37.         MsgBox("Project Information Inserted Successfully.", MsgBoxStyle.Information)
    38.  
    39. End Sub

    With an error occuring on the command.ExecuteNonQuery() line.

    Thanks in advance for any help.
    Last edited by ERage; Dec 20th, 2003 at 06:08 PM.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Just a guess, but there might be something bad with your statement. It's hard to read, anyway. A couple things that might help:

    - Use try...catch - catching a SqlException. Then use the Message property of the SqlExcepton to get some useful message on what's wrong.

    - Use parameters instead of "sql string" & Text1.Text & "more sql stuff" - much easier to read and maintain.

    - Not sure about Access as I don't use it, but do you need the semi-colon at the end of the statement when using a OleDBCommand?

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Couple possibilities:
    Are you inserting into every field in the table? I'm guessing that's what the blanks between single quotes indicate. It might be easier to specify the fields. You have to if you're not putting values in all the fields.
    Make sure the values you insert match the datatypes in the Access table. For example, when inserting a combo.selecteditem, you may want to cstr() it first, if the Access ifield is text.
    Put in a msgbox to view your SQL before executing it, see if it's ok.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2003
    Location
    Savannah, GA
    Posts
    100
    Thanks for the comments. Used the Try Catch method to figure out that one of my datatypes was incorrect and changed my SQL string to include just the fields which were being used which ultimately fixed the problem. Thanks all for helping out a newbie.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width