protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e1)
{
if (e1.CommandName == "BuyClick")
{
Label iid_lbl = (Label)e1.Item.FindControl("iid_lbl");
Label igid_lbl = (Label)e1.Item.FindControl("igid_lbl");
Label iname_lbl = (Label)e1.Item.FindControl("iname_lbl");
Label idesp_lbl = (Label)e1.Item.FindControl("idesp_lbl");
Label iqty_lbl = (Label)e1.Item.FindControl("iqty_lbl");
Label iprice_lbl = (Label)e1.Item.FindControl("iprice_lbl");
Button Button1 = (Button)e1.Item.FindControl("Button1");
str1 = "insert into orders values(' ',' ','" + igid_lbl.Text + "',' ',' ','" + iname_lbl.Text + "',' ')";
str2 = "insert into order_item values(' ','" + iid_lbl.Text + "','" + idesp_lbl.Text + "',' ','" + iprice_lbl.Text + "','" + iqty_lbl.Text + "','" + iname_lbl.Text + "')";
cmd1 = new MySqlCommand(str1, con1);
cmd2 = new MySqlCommand(str2, con1);
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
con1.Close();
Response.Redirect("buy.aspx");
}
}
錯誤發生的實例在str1 = "insert into orders values(' ',' ','" + igid_lbl.Text + "',' ',' ','" + iname_lbl.Text + "',' ')";
System.NullReferencedException發生。對象引用不設置到對象
注意:你應該避免構建這樣的查詢。很難閱讀(並且維護)並且絕對不安全(SQL注入)。使用諸如'@ itemName'之類的參數,然後使用'MySqlCommand'參數替換它。 –