0

我需要为@email 和下拉列表中的基础使用会话数据表电子邮件值。

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);
}

}

4

1 回答 1

0

您读取值的语法Session是错误的,您不能使用Session.dt.email.

您需要读取DataTableoutSession并将其转换为 a 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();
}

现在您可以email在 SQL 命令参数中使用字符串值,如下所示:

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在数据库参数逻辑中的值应该是用户选择的值。

于 2013-08-23T15:45:26.967 回答