我需要使用@email的会话dataTable电子邮件值和下拉列表中的基数。
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = (DataTable)Session["dt"];
DropDownList1.DataValueField = "base";
DropDownList1.DataTextField = "base";
DropDownList1.DataBind();
}
string str;
protected void Submit_Click(object sender, EventArgs e)
{
if (CheckBox9.Checked == true)
{
str = str + CheckBox9.Text + "x";
}
}
SqlConnection con = new SqlConnection(...);
String sql = "UPDATE INQUIRY2 set Question1 = @str WHERE email = @email AND Base = @base;";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@email", Session.dt.email);
cmd.Parameters.AddWithValue("@str", str);
cmd.Parameters.AddWithValue("@base", DropDownList1.base);
}
}
答案 0 :(得分:0)
您从Session
读取值的语法错误,您无法使用Session.dt.email
。
您需要阅读DataTable
中的Session
并将其转换为DataTable
,如下所示:
DataTable theDataTable = null;
// Verify that dt is actually in session before trying to get it
if(Session["dt"] != null)
{
theDataTable = Session["dt"] as DataTable;
}
string email;
// Verify that the data table is not null
if(theDataTable != null)
{
email = dataTable.Rows[0]["Email"].ToString();
}
现在,您可以在SQL命令参数中使用email
字符串值,如下所示:
cmd.Parameters.AddWithValue("@email", email);
更新:
您需要在Page_Load
中包含下拉列表绑定并检查IsPostBack
,因为当您的代码发布时,它会在每次加载页面时绑定下拉列表,而不仅仅是第一次,因此破坏了用户做出的任何选择。
而是这样做:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.DataSource = (DataTable)Session["dt"];
DropDownList1.DataValueField = "base";
DropDownList1.DataTextField = "base";
DropDownList1.DataBind();
}
}
现在,数据库参数逻辑中的base
值应该是用户选择的值。