以下这段代码是输入Id,存入Attending数据库,运行时输入相应的ID,出现如下错误:异常详细信息: System.Data.SqlClient.SqlException: 被准备语句 '(@Id varchar(1),@UCAS varchar(8000))insert into Attending(Id,UCA' 需要参数 @UCAS,但未提供该参数。源错误: 
行 82:  cmds.Parameters.Add("@UCAS",SqlDbType.VarChar);
行 83:  cmds.Parameters["@UCAS"].Value = Session["UCASNumber"];
行 84:                 cmds.ExecuteReader();
行 85: 
行 86: 
 
应该是84行错误,怎样改呢?private void Button1_Click(object sender, System.EventArgs e)
{

            String Id = TextBox2.Text;
try
{
VisitConnection.Open();
SqlCommand cmds = 
new SqlCommand("insert into Attending(Id,UCAS) values (@Id,@UCAS)",VisitConnection); cmds.Parameters.Add("@Id",SqlDbType.VarChar);
cmds.Parameters["@Id"].Value = Id;
cmds.Parameters.Add("@UCAS",SqlDbType.VarChar);
cmds.Parameters["@UCAS"].Value = Session["UCASNumber"];
                cmds.ExecuteReader(); Response.Redirect("UserInterface.aspx");

}
finally{
VisitConnection.Close();
}
}

解决方案 »

  1.   

    是不是Session["UCASNumber"]为null?
    你可以先测试下。
      

  2.   

    是插入数据吗????
    ExecuteReader   ??为什么要用这个????
      

  3.   

    是啊,时插入数据,UCASNumber不是NuLL,没问题,就是不知道要什么参数?
      

  4.   

    完整代码也贴出来,大家看看吧
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace CourseWork3
    {
    /// <summary>

    /// </summary>
    public class WebForm4 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.TextBox TextBox2;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.DataGrid OpenDayTable;
    protected System.Web.UI.WebControls.DataGrid OpenDay; protected System.Data.SqlClient.SqlConnection VisitConnection;

    private void Page_Load(object sender, System.EventArgs e)
    {
    VisitConnection.Open();
    SqlCommand command = new SqlCommand("select * from OpenDay", VisitConnection);
    SqlDataReader reader = command.ExecuteReader();
    OpenDay.DataSource = reader;
    OpenDay.DataBind();
    reader.Close();
    VisitConnection.Close(); } #region 
    override protected void OnInit(EventArgs e)
    {
    //

    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>

    /// </summary>
    private void InitializeComponent() {   
    this.VisitConnection = new System.Data.SqlClient.SqlConnection();
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    // 
    // VisitConnection
    // 
    this.VisitConnection.ConnectionString = "workstation id=erthrc;packet size=4096;integrated security=SSPI;data source=erthr" +
    "c;persist security info=False;initial catalog=VisitDays";
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {

                String Id = TextBox2.Text;
    try
    {
    VisitConnection.Open();
    SqlCommand cmds = 
    new SqlCommand("insert into Attending(Id,UCAS) values (@Id,@UCAS)",VisitConnection); cmds.Parameters.Add("@Id",SqlDbType.VarChar);
    cmds.Parameters["@Id"].Value = Id;
    cmds.Parameters.Add("@UCAS",SqlDbType.VarChar);
    cmds.Parameters["@UCAS"].Value = Session["UCASNumber"];
                    cmds.ExecuteReader(); Response.Redirect("UserInterface.aspx");

    }
    finally{
    VisitConnection.Close();
    }
    }
      

  5.   

    先用UCAS和Password登陆,然后进入这个页面,该页面从OpenDay数据表导入数据Id,和dt的信息,之后,登陆用户输入一个显示出来得Id,然后把Id和这个用户的UCAS都插入到Attending数据表中。