2017-01-07 54 views
0

removebutton_ClickSQL刪除行

protected void removebutton_Click(object sender, EventArgs e) 
{ 
    string RemoveSQL; 

    OleDbConnection mDB = new OleDbConnection(); 
    mDB.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source=" 
          + Server.MapPath("~/App_Data/database.accdb"); 
    mDB.Open(); OleDbCommand cmd; 

    Button removebutton = (Button)sender; 
    Label carttransactionlabel = (Label)removebutton.Parent.FindControl("carttransactionlabel"); 

    int intTransNo = int.Parse(carttransactionlabel.Text); 

    RemoveSQL = "DELETE FROM orderItems" 
       + "WHERE iTransaction_No = @TransNo"; 

    cmd = new OleDbCommand(RemoveSQL, mDB); 

    cmd.Parameters.Add("@TransNo", OleDbType.Integer).Value = intTransNo; 

    cmd.ExecuteNonQuery(); 

    mDB.Close(); 
} 

我想在這裏實現的是,當用戶點擊removebutton按鈕,SQL命令RemoveSQL將從orderItems刪除事務的整個行MS Access中的表根據交易號碼(carttransactionlabel)。

enter image description here

然而,當我嘗試點擊removebutton按鈕,出現此錯誤 enter image description here

顯然有在SQL語句中,我似乎無法發現一個語法錯誤。幫助將不勝感激。

+6

缺少orderItem和WHERE之間的空格 –

回答

2

在運行之前打印出您的SQL!至少有些問題會很明顯。

你有這樣的代碼:

RemoveSQL = "DELETE FROM orderItems" + "WHERE iTransaction_No = @TransNo"; 

如果你打印出來,它看起來像:

RemoveSQL = "DELETE FROM orderItemsWHERE iTransaction_No = @TransNo"; 

錯誤並修復應該是顯而易見的。

注意:您的代碼中可能存在其他問題。