Skip to main content
 首页 » 编程设计

c#之如何将 session 值传递到下一页

2024年02月13日12kenshinobiy

我正在使用 .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(); 

但是我得到了这个错误:

Exception Details: System.Data.SqlClient.SqlException: The parameterized query '(@Email nvarchar(19),@Choice nvarchar(4000))INSERT INTO Choice (' expects the parameter '@Choice', which was not supplied.

请您参考如下方法:

使用AddWithValue而不是 Add 之类的;

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

来自 documentation ;

SqlParameterCollection.Add Method (String, Object) overload has been deprecated. Use AddWithValue(String parameterName, Object value) instead. http://go.microsoft.com/fwlink/?linkid=14202"

或者您可以使用 Add 的 (String, SqlDbType) 重载(我几乎总是喜欢它而不是 AddWithValue)类似的方法(我假设您的列都是 NVarChar 类型);

cmdIns.Parameters.Add("@Email", SqlDbType.NVarChar).Value = userRecord.email; 
cmdIns.Parameters.Add("@Choice", SqlDbType.NVarChar).Value = (string)Session["choice"]; 

也可以使用 using statement处理你的 SqlConnection 和 SqlCommand 之类的;

using(SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"])) 
using(SqlCommand cmdIns = conn.CreateCommand()) 
{ 
   cmdIns.CommandText = sqlIns; 
   ... 
   ... 
}