0

Can some one here tell me what is wrong with my code? Benn spending like an endless hour finding the error.

 protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Insert into CarReserve(UserName,Manufacture,Model,Plate,DateReturn,TimeReturn) Values (@Username1,@Manufacture1,@Model1,@Plate1,@Date1,@TimeReturn1)", conn);

    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@Username1", Label1.Text);
    cmd.Parameters.AddWithValue("@Manufacture1", Label3.Text);
    cmd.Parameters.AddWithValue("@Model1", Label4.Text);
    cmd.Parameters.AddWithValue("@Plate1", Label5.Text);
    cmd.Parameters.AddWithValue("@Datel", Label6.Text);
    cmd.Parameters.AddWithValue("@TimeReturnl", Label7.Text);

    conn.Open();
    cmd.ExecuteNonQuery();
    Response.Redirect("HomeUser.aspx"); 

}

The error is must declare the scalar var @Date1. Thanks in advance.

4

2 回答 2

1
cmd.Parameters.AddWithValue("@Datel", Label6.Text);

看起来您输入@Date**l**的是“字母 ell”而不是@Date**1**“第一”。

看起来您也对以下行犯了同样的错误:

cmd.Parameters.AddWithValue("@TimeReturnl", Label7.Text);
于 2012-09-13T16:18:05.607 回答
-1

据我所知,您应该将参数及其值放在 sql 查询字符串之前,例如:

SqlConnection conn = new SqlConnection  (ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
SqlCommand cmd = new SqlCommand(conn);

cmd.Parameters.AddWithValue("@Username1", Label1.Text);
cmd.Parameters.AddWithValue("@Manufacture1", Label3.Text);

cmd.CommandText = "Insert into CarReserve(UserName,Manufacture,Model,Plate,DateReturn,TimeReturn) Values (@Username1,@Manufacture1,@Model1,@Plate1,@Date1,@TimeReturn1)";

cmd.CommandType = CommandType.Text;
于 2012-09-14T06:08:12.997 回答