locked
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'order'. HELP! Has been an 1 hour trying to figure this out RRS feed

  • Question

  • User-1826017033 posted

    I'm trying to create a simple search and display its data in a gridview.

    Here's my code:

    protected void SearchOrder(object sender, EventArgs e)
        {
            String ConStr = ConfigurationManager.ConnectionStrings["SHOPFORFREE"].ConnectionString;
            SqlConnection con = new SqlConnection(ConStr);
    
            String SQL = "SELECT orderID, username FROM order WHERE username LIKE @search";
    
            SqlCommand cmd = new SqlCommand(SQL, con);
            con.Open();
    
            string invalid = txtSearchOrder.Value;
    
            if (invalid == "")
            {
                lblOrderError.Text = "Please enter partial student Name";
            }
            else
            {
                cmd.Parameters.Add("@search", SqlDbType.NVarChar, 50);
                cmd.Parameters["@search"].Value = "%" + txtSearchOrder.Value + "%";
    
                SqlDataReader reader = cmd.ExecuteReader();
                grdOrder.DataSource = reader;
                grdOrder.DataBind();
    
                reader.Close();
                con.Close();
            }
        }


    Any idea what's wrong?

    Wednesday, July 17, 2013 11:23 AM

Answers

  • User1648350404 posted

    try following:

      String SQL = "SELECT orderID, username FROM [order] WHERE username LIKE @search";
    
    OR
    
    String SQL = "SELECT orderID, username FROM [dbo].[order] WHERE username LIKE @search";



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2013 12:23 PM
  • User-1716253493 posted
    String SQL = "SELECT orderID, username FROM [order] WHERE username LIKE @search";



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2013 12:26 PM

All replies

  • User1648350404 posted

    try following:

      String SQL = "SELECT orderID, username FROM [order] WHERE username LIKE @search";
    
    OR
    
    String SQL = "SELECT orderID, username FROM [dbo].[order] WHERE username LIKE @search";



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2013 12:23 PM
  • User-1716253493 posted
    String SQL = "SELECT orderID, username FROM [order] WHERE username LIKE @search";



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2013 12:26 PM