這是我爲新應用程序的解決方案共享的代碼的一部分。必須聲明標量變量@ TextBox1 asp.net
當我執行這個代碼,它會拋出異常「必須聲明標量變量@ textBox1的」
public partial class About : Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = "Data Source=.;Initial Catalog=carpro;Integrated Security=True";
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Insert into Driver_Registration(Driver_Name,Driver_DOB,Driver_Address,License_Number,National_Insurance_Number,"+
"Email_Address,UK_History,Contact,Occupation,License_Details,Taxi_License_Number,"+
"Deposit_Details,Weekly_Rent) Values (@TextBox1,@TextBox2,@TextBox3,@TextBox4,@TextBox5," +
"@TextBox6,@TextBox7,@TextBox8,@TextBox9,@TextBox10,@TextBox11,@TextBox12,@TextBox13)",con);
cmd.Parameters.AddWithValue("@Driver_Name",TextBox1.Text);
cmd.Parameters.AddWithValue("@Driver_DOB", TextBox2.Text);
cmd.Parameters.AddWithValue("@Driver_Address", TextBox3.Text);
cmd.Parameters.AddWithValue("@License_Number", TextBox4.Text);
cmd.Parameters.AddWithValue("@National_Insurance_Number", TextBox5.Text);
cmd.Parameters.AddWithValue("@Email_Address", TextBox6.Text);
cmd.Parameters.AddWithValue("@UK_History", TextBox7.Text);
cmd.Parameters.AddWithValue("@Contact", TextBox8.Text);
cmd.Parameters.AddWithValue("@Occupation", TextBox9.Text);
cmd.Parameters.AddWithValue("@License_Details", TextBox10.Text);
cmd.Parameters.AddWithValue("@Taxi_License_Number", TextBox11.Text);
cmd.Parameters.AddWithValue("@Deposit_Details", TextBox12.Text);
cmd.Parameters.AddWithValue("@Weekly_Rent", TextBox13.Text);
cmd.ExecuteNonQuery();
Label1.Text = "Registration Successfull";
con.Close();
}
}
cmd.Parameters.AddWithValue( 「@ TextBox1中」,TextBox1.Text); –
切勿將'SqlCommand'或'SqlConnection'作爲字段存儲。也可以將它作爲本地函數使用,並在finally塊中將其「Dispose」或用wrap語句包裝。 – mason