Incorrect syntax near ' '

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thomaaxha
    New Member
    • Mar 2013
    • 16

    Incorrect syntax near ' '

    hi ,, i have a problem in my web app .. i have a calendar and i want to get the day number id from a table and to put it in another table. I want to find my error , please help me. the code is like this:
    Code:
    String day ="SELECT ID FROM Ditet WHERE Dita = ('" + Calendar1.SelectedDate.Day  + "') ";
            SqlCommand day1  = new SqlCommand (day , con);
            string day3 = day1.ExecuteScalar().ToString();
         String vizita = "INSERT INTO [Vizita] (ID_dita)  VALUES  ('" + day3 + "')";
         SqlCommand cmd1 = new SqlCommand( vizita, con);
         cmd1.ExecuteScalar();
  • nicolcrome
    New Member
    • Apr 2013
    • 2

    #2
    It may be single quotes problem , just replace single quotes with double quotes

    return str.Replace("'" , "''");

    follow th link

    The system shows the above error messages, because we are trying to insert a single quoted character using in sql statement


    crome.

    Comment

    • vijay6
      New Member
      • Mar 2010
      • 158

      #3
      Hey thomaaxha, your code don't have errors (i tested, works fine). Except you were opened and closed the connection to your database. In line number 6 in your code you should use ExecuteNonQuery () instead of ExecuteScalar() .

      Comment

      • thomaaxha
        New Member
        • Mar 2013
        • 16

        #4
        thnx for the advice ,, wish the best 4 u:)

        Comment

        Working...