如何将会话值传递给下一页?

时间:2014-05-02 11:47:46

标签: c# asp.net sql .net ado.net

我正在使用.net,我正在更改我的.aspx.cs文件,将单选按钮中的用户选项添加到数据库中的一行。

 private DataSet CreateData()
    {
        DataTable table = new DataTable();
        table.Columns.Add(new DataColumn("OptionID", System.Type.GetType("System.String")));
        table.Columns.Add(new DataColumn("Option", System.Type.GetType("System.String")));

        DataRow row1 = table.NewRow();
        row1["OptionID"] = "1";
        row1["Option"] = "option 1";

        DataRow row2 = table.NewRow();
        row2["OptionID"] = "2";
        row2["Option"] = "two";

        DataRow row3 = table.NewRow();
        row3["OptionID"] = "3";
        row3["Option"] = "three";

        table.Rows.Add(row1);
        table.Rows.Add(row2);
        table.Rows.Add(row3);

        DataSet ds = new DataSet();
        ds.Tables.Add(table);

        return ds;

    }

    private void bindRadioList()
    {
        DataSet ds = CreateData();
        RadioButtonList1.DataSource = ds;
        RadioButtonList1.DataTextField = "Option";
        RadioButtonList1.DataValueField = "OptionID";
        RadioButtonList1.DataBind();

        if (RadioButtonList1.Items.Count > 0)
            RadioButtonList1.Items[0].Selected = true; //you can set a selected items you want.


    }

    public void Button1_Click(object sender, System.EventArgs e)
    {
        Session["choice"] = RadioButtonList1.SelectedValue;

    }

但我不知道当人们在下一页点击提交时,我怎么能在最后一步获得这个值。 我试过了:

  string sqlIns = "INSERT INTO Choice (Email, Choice) VALUES (@Email, @Choice)";
             SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
                conn.Open();
            try
            {
                SqlCommand cmdIns = new SqlCommand(sqlIns, conn);
                cmdIns.Parameters.Add("@Email", userRecord.email);
                cmdIns.Parameters.Add("@Choice", (string)Session["choice"]);
                cmdIns.ExecuteNonQuery();

但我收到了这个错误:

  

异常详细信息:System.Data.SqlClient.SqlException:The   参数化查询'(@Email nvarchar(19),@ Choice   nvarchar(4000))INSERT INTO Choice('期望参数'@Choice',   没有提供。

3 个答案:

答案 0 :(得分:4)

使用AddWithValue代替Add;

cmdIns.Parameters.AddWithValue("@Email", userRecord.email);
cmdIns.Parameters.AddWithValue("@Choice", (string)Session["choice"]);

来自documentation;

  

SqlParameterCollection.Add Method (String, Object)已超载   弃用。使用AddWithValue(String parameterName, Object value)   代替。 http://go.microsoft.com/fwlink/?linkid=14202

或者您可以使用(String, SqlDbType)重载(我几乎总是更喜欢而不是AddWithValueAdd方法(我假设您的列都是{ {1}}类型);

NVarChar

还可以使用using statement来处理您的cmdIns.Parameters.Add("@Email", SqlDbType.NVarChar).Value = userRecord.email; cmdIns.Parameters.Add("@Choice", SqlDbType.NVarChar).Value = (string)Session["choice"]; adn SqlConnection;

SqlCommand

答案 1 :(得分:0)

请检查您在哪里传递参数的商店程序,你没有传递参数@Choice

答案 2 :(得分:0)

我发现了问题和解决方案: 问题是Session [“choice”] = Null,在MSN标准下是不可接受的。所以我需要添加它以防止Null值:

   if ((string)Session["choice"] == null)
                {
                    cmdIns.Parameters.Add("@Choice", SqlDbType.NVarChar).Value = DBNull.Value;
                }
                else
                {
                   // cmdIns.Parameters.AddWithValue("@Choice", (string)Session["choice"]);
                    cmdIns.Parameters.Add("@Choice", SqlDbType.NVarChar).Value = (string)Session["choice"];
                }

要修复.aspx文件中的空值(我仍然无法弄清楚为什么旧代码为null,但这个可以避免它)

   <div>                                                                                                                               
              <asp:RadioButton  ID="RadioButton1" GroupName="Group1" Text="Choice 1" OnCheckedChanged="Button1_Click" AutoPostBack="true" runat="server" />
              <asp:RadioButton  ID="RadioButton2" GroupName="Group1" Text="Choice 2" OnCheckedChanged="Button1_Click" AutoPostBack="true" runat="server" /> 
              <asp:RadioButton  ID="RadioButton3" GroupName="Group1" Text="Choice 3" OnCheckedChanged="Button1_Click" AutoPostBack="true" runat="server" />
        </div>

和.cs文件:

protected void Button1_Click(object sender, System.EventArgs e)
        {
           // Session["choice"] = RadioButtonList1.SelectedItem.Text;
            if (RadioButton1.Checked)
            {
                Session["choice"] = "Choice 1";
            }

            if (RadioButton2.Checked)
            {
                Session["choice"] = "Choice 2";
            }
            if (RadioButton3.Checked)
            {
                Session["choice"] = "Choice 3";
            }

        }