-2
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = (DataTable)Session["dt"];
            DropDownList1.DataValueField = "base";
            DropDownList1.DataTextField = "base";
            DropDownList1.DataBind();
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {


    }

    string str;
    string email;
    string base1;

    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;";

    SqlCommand cmd = new SqlCommand(sql, con);
    con.Open();

    DataTable theDataTable = null;

       // Verify that dt is actually in session before trying to get it

        if(Session["dt"] != null)
        {
            theDataTable = Session["dt"] as DataTable;
        }



    //Verify that the data table is not null
    if(theDataTable != null)
    {
        email = theDataTable.Rows[0]["email"].ToString();
        base1 = theDataTable.Rows[0]["base"].ToString();
    }

    cmd.Parameters.AddWithValue("@email", email);
    cmd.Parameters.AddWithValue("@str", str);
    cmd.Parameters.AddWithValue("@base", base1);

    con.Close();  

    }

}

}

4

1 回答 1

0

您缺少实际命令的执行调用:

cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@str", str);
cmd.Parameters.AddWithValue("@base", base1);

cmd.ExecuteNonQuery(); // <--- this line here

con.Close(); 

我还强烈建议您将连接包装在 a 中using,以免意外将其打开:

using(SqlConnection con = new SqlConnection(...))
{
    con.Open();

    /*
        rest of code here
    */

    con.Close();
}
于 2013-08-23T18:19:38.830 回答