rai nalasa rai nalasa - 6 months ago 18
SQL Question

System.Data.SqlClient.SqlException on multiple WHERE query

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.

Answer

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'
Comments