Please help!

Discussion in 'C#' started by JEP_Dude, Apr 22, 2010.

  1. #1
    Hey there...

    I think I may have found a possible problem within the SQL portion of my VB.Net 2008 app. Yet, I'm kinda new to this.

    On the "MessageBox.Show(ex.Message)" line I receive the SQL error that reads:

    Incorrect syntax near the keyword 'Statistics'.

    Therefor it says the line where i put: WHAT IS WRONG WITH THIS LINE? However, please note that I used a continuation underscore on this line.

    Thanks a lot!

    ---- Here's the procedure....

    Friend Sub SaveToSQL()

    Dim ConnectionString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename= ...;
    User Instance=True"

    Dim SQL_Connection As New SqlClient.SqlConnection(ConnectionString)

    SQL_Connection.Open() ' I need to redo the connection string!

    Dim SQL_Command As New SqlClient.SqlCommand("INSERT INTO Statistics VALUES (?, ?)", _
    SQL_Connection) <------------------------------------------------ WHAT IS WRONG WITH THIS LINE?

    For L_Index = 1 To (G_NIP - G_MissingStock)
    For L_Day = 1 To G_MaxSampleSize
    G_StockIndex += 1

    SQL_Command.Parameters.Add(New SqlClient.SqlParameter("StockIndex", G_StockIndex))
    Try
    SQL_Command.ExecuteNonQuery()
    Catch ex As Exception
    MessageBox.Show(ex.Message) <---- Here's where the error is shown!
    End Try

    Next L_Day
    Next L_Index

    ' @@@@ Both of these two values NEED to be verified!
    SQL_Connection.Close()
    SQL_Connection.Dispose()

    End Sub

    ----

    May you have a blessed day as the Lord wills.

    JEP_Dude
     
    JEP_Dude, Apr 22, 2010 IP
  2. cDc

    cDc Peon

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think your query may be using a reserved word - try this instead

    INSERT INTO [Statistics] VALUES (?, ?)
     
    cDc, Apr 22, 2010 IP
  3. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Also, make sure to replace ticks and quotes.
     
    ccoonen, Apr 24, 2010 IP
  4. JEP_Dude

    JEP_Dude Peon

    Messages:
    121
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Dear Sir ....

    Thanks for your reply!

    Yet my SQL experience is so limited that I must ask for you to further explain what you mean when you said, "tics and quotes". Otherwise, I'm at a loss.

    May you and everyone else have a blessed day.

    JEP_Dude
     
    JEP_Dude, Apr 25, 2010 IP
  5. bibinsmk

    bibinsmk Active Member

    Messages:
    205
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Place the table name in [] . And also check the input values are correct.
     
    bibinsmk, Apr 26, 2010 IP
  6. kylecorley

    kylecorley Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you ever have a SQL statement containing the single quote (') (a.k.a., apostrophe or "tick" mark) or double quote ("), SQL Server can interpret them incorrectly unless you escape them. These two links should help you:

    SQL Server Documentation on SET QUOTED_IDENTIFIER

    How to Escape Single Quotes
     
    kylecorley, Apr 27, 2010 IP