我想對按鈕單擊事件執行查詢。從另一個函數執行查詢
但是,該查詢是寫在另一個函數。
這是我的代碼,它不工作。我的問題是什麼?
namespace MCE_Member_Registration
{
public partial class registration_form_view : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("ConnectionString");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
createform();
}
protected void createform() {
NameValueCollection nvc = Request.Form;
surname.Text = nvc["txt_surname"];
cmd.CommandText = "Insert into mce_applicants_information values(N'" + nvc["txt_surname"] + "')";
}
protected void confirm_Click(object sender, EventArgs e)
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
此查詢執行空。我認爲這是因爲SqlCommand cmd的東西是在另一個功能... – user2163530 2013-03-14 09:37:53
這是一個糟糕的設計,因爲你正在設置一個對象在課堂上。 – 2013-03-14 09:38:17