2015-07-10 83 views
1

我在字符串''之後得到未封閉的引號。我已經嘗試了一切幫助將不勝感激。C#Sql服務器更新與多個where子句和多個id字段

 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sipConnectionString"].ConnectionString); 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    conn.Open(); 
    string query = "select dealercode, dropdate, couponno from coupon where dealercode = '" + DEALERCODETextBox.Text + "' and dropdate = '" + DROPDATETextBox.Text + "' and COUPONNO = '" + COUPONCOUNTTextBox.Text +"','"; 
    SqlCommand cm = new SqlCommand(query, conn); 
    cm.Parameters.AddWithValue("@couponcount", COUPONCOUNTTextBox.Text); 
    cm.Parameters.AddWithValue("@totalrev", GRANDTOTALTextBox.Text); 
    cm.ExecuteNonQuery(); 
    conn.Close(); 

回答

0

替換這一行:

string query = "select dealercode, dropdate, couponno 
    from coupon where dealercode = '" + DEALERCODETextBox.Text + "' 
    and dropdate = '" + DROPDATETextBox.Text + "' 
    and COUPONNO = '" + COUPONCOUNTTextBox.Text +"'"; 
SqlCommand cm = new SqlCommand(query, conn); 
+0

感謝您的答覆我修改了代碼,你的建議我的錯誤已被清除,但是當我點擊提交按鈕的所有文本框填寫表單永遠不會將任何信息添加到數據庫有沒有辦法找到什麼不是當點擊提交按鈕時發生的事情 –

3

在過去的查詢字符串

and COUPONNO = '" + COUPONCOUNTTextBox.Text +"','"; 

"'";

注意更換+"','";:您的查詢字符串也缺乏Parameters

1

您使用paramters添加值,但你不要在查詢中使用的參數:

string query = "select dealercode, dropdate, couponno from coupon where dealercode = @dealercode and dropdate [email protected] and COUPONNO = @couponcount;"; 
SqlCommand cm = new SqlCommand(query, conn); 
cm.Parameters.AddWithValue("@couponcount", COUPONCOUNTTextBox.Text); 
cm.Parameters.AddWithValue("@dealercode ", DEALERCODETextBox.Text); 
cm.Parameters.AddWithValue("@dropdate ", DROPDATETextBox.Text);