locked
Incorrect syntax near '('. RRS feed

  • Question

  • ERROR: 

    Incorrect syntax near '('.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '('.

    Source Error: 

    Line 52:   protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    Line 53:   {
    Line 54:     objds2.Insert();
    Line 55:     /*BusinessObjects.Subscription subs = new BusinessObjects.Subscription();
    Line 56:     string gender = "";

    //Code.aspx.cs
    protected void objds2_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            string gender = "";
            int id;
            SqlConnection conn = new SqlConnection("Data Source=LEITHON-PC;Initial Catalog=0801150-GameArcade;Integrated Security=True");
            SqlCommand cmd0 = new SqlCommand("SELECT MAX(CustomerID) FROM Customer", conn);
            id = Convert.ToInt32(cmd0.ExecuteScalar());
            if(rbt_male.Checked == true)
            {
                gender = "M";
            }
            else if(rbt_fmale.Checked == true)
            {
                gender = "F";
            }
           e.InputParameters.Clear();
           e.InputParameters.Add("fname", txt_fname.Text);
           e.InputParameters.Add("lname", txt_lname.Text);
           e.InputParameters.Add("dob", txt_date.Text);
           e.InputParameters.Add("job", txt_Job.Text);
           e.InputParameters.Add("gender", gender);
           e.InputParameters.Add("sub", ddl_subs.SelectedItem);
           e.InputParameters.Add("passcode", txt_passconfirm.Text);
           e.InputParameters.Add("pgtype", ddl_GType.SelectedItem);
           e.InputParameters.Add("cid", id);
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            objds2.Insert();
        }


    //Customers.cs

    public void AddCustomer(int cid, string fname, string lname, string dob, string job, string gender, string sub, string pass, string pgtype)
       {
           DateTime DOB = Convert.ToDateTime(dob);
           SqlConnection conn = new SqlConnection("Data Source=LEITHON-PC;Initial Catalog=0801150-GameArcade;Integrated Security=True");
           SqlCommand cmd = new SqlCommand("INSERT INTO Customer(First_Name, Last_Name, DOB, Job, Gender, Deleted"
                                           + "VALUES(@First_Name, @Last_Name, @DOB, @Job, @Gender, 'F')", conn);

           cmd.Parameters.Add("@First_Name", SqlDbType.VarChar, 25);
           cmd.Parameters["@First_Name"].Value = fname;
           cmd.Parameters.Add("@Last_Name", SqlDbType.VarChar, 25);
           cmd.Parameters["@Last_Name"].Value = lname;
           cmd.Parameters.Add("@DOB", SqlDbType.Date, 3);
           cmd.Parameters["@DOB"].Value = DOB.Date;
           cmd.Parameters.Add("@Job", SqlDbType.Text, 2147483647);
           cmd.Parameters["@Job"].Value = job;
           cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 1);
           cmd.Parameters["@Gender"].Value = gender;
           conn.Open();
           cmd.ExecuteNonQuery();

           SqlCommand cmd0 = new SqlCommand("SELECT MAX(CustomerID) FROM Customer", conn);

           SqlCommand cmd1 = new SqlCommand("INSERT INTO CustomerAC(CustomerID, Sub_Type, PassCode, PreferredG_Type, Purchases_To_Date, Cost_of_PDT"
                                           + "VALUES(@CustomerID, @Sub_Type, @PassCode, @PGType, 0, 0)", conn);
           cmd1.Parameters.Add("@CustomerID", SqlDbType.Int, 1);
           cmd1.Parameters["@CustomerID"].Value = cid;
           cmd1.Parameters.Add("@Sub_Type", SqlDbType.VarChar, 30);
           cmd1.Parameters["@Sub_Type"].Value = sub;
           cmd1.Parameters.Add("@PassCode", SqlDbType.VarChar, 16);
           cmd1.Parameters["@PassCode"].Value = pass;
           cmd1.Parameters.Add("@PGType", SqlDbType.VarChar, 10);
           cmd1.Parameters["@PGType"].Value = pgtype;
           cmd1.ExecuteNonQuery();
           conn.Close();
       }
    Wednesday, November 24, 2010 2:17 PM

Answers

  • It is a little hard to tell from the information you have provided.  but, here are two things you should change.

     

    In the Insert statements you are joining two strings together but there is no space separating the last column name from the VALUES keyword.

     

    So, your inserts are probably the problem.

     

    SqlCommand cmd = new SqlCommand("INSERT INTO Customer(First_Name, Last_Name, DOB, Job, Gender, Deleted"
                                           + "VALUES(@First_Name, @Last_Name, @DOB, @Job, @Gender, 'F')", conn);

    and

    SqlCommand cmd1 = new SqlCommand("INSERT INTO CustomerAC(CustomerID, Sub_Type, PassCode, PreferredG_Type, Purchases_To_Date, Cost_of_PDT"
                                           + "VALUES(@CustomerID, @Sub_Type, @PassCode, @PGType, 0, 0)", conn);


    jon.stromer.galley
    • Marked as answer by lae_jw Wednesday, November 24, 2010 2:30 PM
    Wednesday, November 24, 2010 2:27 PM

All replies

  • You sometimes get that error if nothing is changed, and thus nothing is updated
    Wednesday, November 24, 2010 2:20 PM
  • SqlCommand cmd1 = new SqlCommand("INSERT INTO CustomerAC(CustomerID, Sub_Type, PassCode, PreferredG_Type, Purchases_To_Date, Cost_of_PDT)"
                                           + " VALUES(@CustomerID, @Sub_Type, @PassCode, @PGType, 0, 0)", conn);

    you forgot a ) in both queries i think
    Wednesday, November 24, 2010 2:23 PM
  • It is a little hard to tell from the information you have provided.  but, here are two things you should change.

     

    In the Insert statements you are joining two strings together but there is no space separating the last column name from the VALUES keyword.

     

    So, your inserts are probably the problem.

     

    SqlCommand cmd = new SqlCommand("INSERT INTO Customer(First_Name, Last_Name, DOB, Job, Gender, Deleted"
                                           + "VALUES(@First_Name, @Last_Name, @DOB, @Job, @Gender, 'F')", conn);

    and

    SqlCommand cmd1 = new SqlCommand("INSERT INTO CustomerAC(CustomerID, Sub_Type, PassCode, PreferredG_Type, Purchases_To_Date, Cost_of_PDT"
                                           + "VALUES(@CustomerID, @Sub_Type, @PassCode, @PGType, 0, 0)", conn);


    jon.stromer.galley
    • Marked as answer by lae_jw Wednesday, November 24, 2010 2:30 PM
    Wednesday, November 24, 2010 2:27 PM
  • thank you! I didn't rectify the whole problem but it did help

     

    Wednesday, November 24, 2010 2:30 PM
  • First, note that it's a SqlException, so it's not on the actual line that you have highlighted, it's in THAT method.

     I think the problem is in the AddCustomer method - specifically this line:

     SqlCommand cmd = new SqlCommand("INSERT INTO Customer(First_Name, Last_Name, DOB, Job, Gender, Deleted"
              + "VALUES(@First_Name, @Last_Name, @DOB, @Job, @Gender, 'F')", conn);
    
    

    You don't have the matching RIGHT PARENTHESIS for the left one in the first part of the INSERT INTO statement.  Note that 'Deleted' is followed by VALUES. 

    It should be something like:

     SqlCommand cmd = new SqlCommand("INSERT INTO Customer(First_Name, Last_Name, DOB, Job, Gender, Deleted) "
              + "VALUES(@First_Name, @Last_Name, @DOB, @Job, @Gender, 'F')", conn);
    
    

    Note that I added a right paren AND a space.

    And, not trying to be confrontational, but I've never personally gotten a SQL exception for an update that updates nothing.  I don't see how SQL would throw an error; it's set-based logic, and the empty set is perfectly valid.

    Thanks,

    Phil

    Wednesday, November 24, 2010 2:49 PM