0

I am using my quo_custname that is bind in my dropdownlist and use its value to Insert my data.So whenever I choose a value on my dropdownlist quo_name change but whenever I use my class for mysql Insertion.

I get this error Additional information: Incorrect syntax near the keyword 'WHERE'.

Here is my code:

 private void AddToQuotation()
    {
        con.Open();
        cmd = new SqlCommand(@"INSERT INTO JobQuotations
                            (quo_disount,quo_unitPrice,quo_TotalAmount,quo_finishing)
                             VALUES
                            ('@discount','@unitPrice','@totalAmount'
                            ,'@finishing')
                            WHERE quo_custname = '@customername'
                            AND quo_verified = 'no'",con);

        cmd.Parameters.AddWithValue("@customername",DropDownList3.SelectedItem.Value.ToString());
        cmd.Parameters.AddWithValue("@discount",txtDisc.Text);
        cmd.Parameters.AddWithValue("@unitPrice",txtUnitPrice.Text);
        cmd.Parameters.AddWithValue("@totalAmount",lblTotalAmount.Text);
        cmd.ExecuteNonQuery();
        con.Close();
        string script = "alert(\"Functioning!\");";

        ScriptManager.RegisterStartupScript(this, GetType(),
            "ServerControlScript", script, true);
    }

*Bonus question also how could I using try and catch so that it wouldn't break my program and get the SqlException name and the Additional information in my script to popup a message.

2
  • stackoverflow.com/questions/17463904/… Commented May 10, 2016 at 9:37
  • write your parameters @customername instead of '@customername' otherwise they will be recognized as string Commented May 10, 2016 at 9:40

4 Answers 4

3

Your statement doesn't make much sense to me. You are mixing insert into select syntax with insert into values syntax. Also, you are using quotes around your parameters, which won't work (they are interpreted as literals now).

Are you sure you didn't mean to update?

update JobQuotations
set    quo_disount     = @discount
,      quo_unitPrice   = @unitPrice
,      quo_TotalAmount = @totalAmount
,      quo_finishing   = @finishing
where  quo_custname    = @customername
and    quo_verified    = 'no'
Sign up to request clarification or add additional context in comments.

4 Comments

No I'm not updating.
So what is the purpose of the where then?
I am trying to insert this sqldata in my null columns.Their initial value was Null.Example : totalAmount is null so I need to insert value on it in order to have its price after I calculated it.
So that is an update right? The columns were null, so there is a row.
0

It seems you are making an insert statement while also specifying a WHERE condition. SQL inserts typically do not use WHERE conditions so you probably want to remove that part.

Comments

0

for this use update query that you have record in database.

1 Comment

There was already a comprehensive answer that goes into detail. No need for this comment.
0

You are using a WHERE clause in an INSERT statement, it will not work because we can't use a where clause with an INSERT statement. Either you have to use :

INSERT INTO JobQuotations(quo_disount,quo_unitPrice,quo_TotalAmount,quo_finishing)
                  VALUES('@discount','@unitPrice','@totalAmount','@finishing')

or

UPDATE statement (if you want to use WHERE).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.