HELP! What is wrong with INSTERT?

Discussion in 'C#' started by JEP_Dude, Jun 9, 2010.

  1. #1
    Hey there....

    I'm trying to learn VS.Net 2008 on my own. It seems very much like an up hill climb without a rope.

    I'm trying to save the results I've calculated to a SQL Server 2008 MDF file. I've called this simple DB DBTest and it only has two fields (columns). The first is the primary key and its called StockIndex (int, No Nulls) and the second is called StockSymbol (nchar(10), Allows Nulls).

    I've been lead to believe that the INSERT command is well suited for what I want to do, but I keep getting the same error! ...and its getting to me! The error message is:

    Incorrect syntax near '?'.

    Any help you can give would be greatly appreciated!

    May each of you have a blessed day as the Lord wills.

    John

    Here's an example of my code:
     
    JEP_Dude, Jun 9, 2010 IP
  2. cDc

    cDc Peon

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You usually use the @ sign for params in SQL Server - like this INSERT INTO DBTest (StockIndex, StockSymbol) VALUES (@StockIndex, @StockSymbol)
     
    cDc, Jun 11, 2010 IP
  3. JEP_Dude

    JEP_Dude Peon

    Messages:
    121
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi ....

    I'm still getting errors when the program gets to this line.

    I was thinking of changing the above line from

    Dim SQL_Command As New SqlClient.SqlCommand("INSERT INTO DBTest (StockIndex, StockSymbol) VALUES (?, ?)", SQL_Connection)

    to use single quote around the column names like in this:

    Dim SQL_Command As New SqlClient.SqlCommand("INSERT INTO DBTest ('StockIndex', 'StockSymbol') VALUES (?, ?)", SQL_Connection)

    What should I expect?

    God bless

    JEP_Dude
     
    JEP_Dude, Jun 12, 2010 IP
  4. sauravmandhotra

    sauravmandhotra Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    use @ for ur paramerter
     
    sauravmandhotra, Jul 30, 2010 IP
  5. chanda

    chanda Peon

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    u should put the parameters into single quotes and then in inverted comas
     
    chanda, Aug 5, 2010 IP
  6. JEP_Dude

    JEP_Dude Peon

    Messages:
    121
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    chanda ....

    Thanks for your assistance. Yet, I'm new with SQL. So please forgive me for asking but, what do you mean when you said, "... put the parameters into single quotes and then in inverted comas".

    To clarify, would you please give me an example of wht you mean with the line below?

    Dim SQL_Command As New SqlClient.SqlCommand("INSERT INTO DBTest ('StockIndex', 'StockSymbol') VALUES (?, ?)", SQL_Connection)

    Thanks in advance.

    May you, as well as those listening, have a blessed day as the Lord wills.

    JEP_Dude
     
    JEP_Dude, Aug 7, 2010 IP
  7. bhuppi890109

    bhuppi890109 Member

    Messages:
    1,337
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    48
    #7
    i m not sure but u should do one of the followings...
    1.put stockindex into inverted comas and stock symbol into single quotes and then in inverted comas such as ("Stockindex",'"StockSymbol"')
    2.or put first ?in to inverted comas and second ? into single quotes and then into inverted comas as given in above e.g
    3.if both two steps given is not worked than do both steps simultaneously....

    and note one thing parameters of int always come in inverted comas and parameters for char comes in single quotes then in inverted comas
     
    bhuppi890109, Aug 8, 2010 IP
  8. nagarajang

    nagarajang Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can try out this...


    Dim SQL_Command As New SqlClient.SqlCommand("INSERT INTO DBTest ('StockIndex', 'StockSymbol') VALUES ("& G_StockIndex)&", "& G_Stock(1, 1).Symboll &")", SQL_Connection)
     
    nagarajang, Aug 12, 2010 IP
  9. chanda

    chanda Peon

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    write
    ('?','?')
     
    chanda, Aug 19, 2010 IP
  10. JEP_Dude

    JEP_Dude Peon

    Messages:
    121
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hey there ...!

    I've tried a suggestion given before and it works well! It was suggested that I follow the older approach given within SQL 2005. Yet, the question does remain of, how much of SQL 2005 is still valid within SQL 2008? If each command of the previous version still works, then why change anything?

    May you have a blessed day as the Lord wills.

    JEP_Dude
     
    JEP_Dude, Aug 19, 2010 IP
  11. mohsinpatel

    mohsinpatel Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thanks all for sharing ..learn much
     
    mohsinpatel, Dec 21, 2010 IP