none
C# SQL Query Syntax Problem RRS feed

  • Question

  • Hi. I am trying to insert Data to my database constructed by OLEDB.

    I am trying to add two TextField and a ComboBox value. However , I encountered with several problem. Apparantly the problem is with the string i use. i make some syntax errors but i dont know what.
     dataAdapter.InsertCommand.CommandText =
                    "INSERT INTO Tablo1 " +
                            "(curGpaField , curCreditField ) " +
                    " VALUES (@curGpaField,@curCreditField )";

    curGpaField and curCreditField are TextFields so there is no problem. But when i insert combobox;

     dataAdapter.InsertCommand.CommandText =
                    "INSERT INTO Tablo1 " +
                            "(curGpaField , curCreditField,comboBox1 ) " +
                    " VALUES (@curGpaField,@curCreditField , @comboBox1)";

    i tried writing comBox1.Text , comboBox1.Text.ToString() but they were of no use.

    it doesnt work. how is the syntax supposed to be ? what is the meaning of @ there, by the way? i am a beginner can you please help me ?



    Friday, September 14, 2007 4:03 PM

Answers

  • Instead of simply mentionning that you solved the problem, you could have also posted *how* you solved the problem.. This way other people with the same problem can learn from you. (Wouldn't that be cool? Wink)

    Anyway, the @ is used to indicate a variable in a SQL-query, thus in your case your query would have been something as:


    Code Snippet

    sqlCommand.CommandText = "INSERT INTO table ( col1 ) VALUES ( @val1 ) ";
    sqlCommand.Parameters.Add("@val1", SqlDbType.Varchar).Value = comboBox1.SelectedValue.ToString();


    Sunday, September 16, 2007 3:43 PM

All replies

  • Hmmm... please send the Error Message when your script executed in order to analyze for your solution.
    Saturday, September 15, 2007 4:41 AM
  • thanks wiltek. the problem is solved. shame on me Sad
    its my fault Smile
    Saturday, September 15, 2007 4:53 AM
  • Instead of simply mentionning that you solved the problem, you could have also posted *how* you solved the problem.. This way other people with the same problem can learn from you. (Wouldn't that be cool? Wink)

    Anyway, the @ is used to indicate a variable in a SQL-query, thus in your case your query would have been something as:


    Code Snippet

    sqlCommand.CommandText = "INSERT INTO table ( col1 ) VALUES ( @val1 ) ";
    sqlCommand.Parameters.Add("@val1", SqlDbType.Varchar).Value = comboBox1.SelectedValue.ToString();


    Sunday, September 16, 2007 3:43 PM
  • Thanks timvw , actually you ' re right. However, I discovered that my problem had nothing to do with this syntax but there was something different in my code that was troubling me. It was about something that is totally different. So i thought it wouldnt be worth to post it. Because its not a solution actually. But,really thanks for the advice.

    And as a tip, never consider the problem by just focusing on the error lines the compiler states. That was my fault
    Sunday, September 16, 2007 3:53 PM
  • May I ask something different under the same thread ?

    I want to update a row from the database but when the user clicks it i dont know which row to update. Its simply gonna be the row whose name key equals to the name which user inputs.

    But at the beginning of the code we write something

    DataRow targetRow; = dataTable.Rows[1];   in which 1 implies the index of the data that is gonna be updated. But i dont know it yet.I wrote my sql command as

     dataAdapter.UpdateCommand.CommandText ="UPDATE Tablo1" +
                    " SET curGpaField = @curGpaField  WHERE name = @name";

    but because of the line above , it only updates when the user enters the first rows name key. What am i supposed to do to learn the index of the row at the very beginning. Or is there any other method to use to sort this problem out.
              
    Thanks in advance
    Sunday, September 16, 2007 4:11 PM
  •  

    May I ask something different under the same thread ?

     

    I suggest that you open a new thread with a clear subject for the new question, so it is more likely to be answered.

    Tuesday, September 18, 2007 1:46 AM