根据下拉列表选择更新数据库?

时间:2018-02-05 16:12:21

标签: c# asp.net

我有一个名为SisStatus的表,它包含两列" StatusID"和"状态",它包含四行,"正常","撤回","暂时撤回"和"暂停&# 34 ;.

我已拉出此表并将其显示在下拉列表中,其中包含以下代码: -

ASP.NET

      <asp:DropDownList ID="ddlStatus" runat="server">
                                    </asp:DropDownList>

C#

 private void FillDropDownList()
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["scConnection"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();

        string dropDownQuery = "SELECT * FROM SisStatus";

        SqlCommand cmd = new SqlCommand(dropDownQuery, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        da.Fill(ds);

        ddlStatus.DataTextField = ds.Tables[0].Columns["Status"].ToString();
        ddlStatus.DataValueField = ds.Tables[0].Columns["Status"].ToString();

        ddlStatus.DataSource = ds.Tables[0];
        ddlStatus.DataBind();
    }

这样可以正常工作,并且正在填充下拉列表。当我尝试更新不同的表时,会出现问题SisStudents&#39; Status&#39;列。

以下是我用来尝试更新它的代码,但它不起作用。唯一可行的方法是,如果在数据库中(通过我已经将其放入),状态是“撤回”,“暂时撤回”,“暂停”,以及您将状态更改为&#39;正常&#39;它有效...

protected void UpdateBtnClick(object sender, EventArgs e)


{

    /**
   * When clicked takes the data the user has entered
   * and updates their row in the db
   */

    string newForename = txtForename.Text;
    string newSurname = txtSurname.Text;
    string newTermAddress = txtTermAddress.Text;
    string newHomeAddress = txtHomeAddress.Text;
    string newPhone = txtPhoneNumber.Text;
    string newDOB = txtDOB.Text;
    string newContactName = txtContactName.Text;
    string newContactAddress = txtContactAddress.Text;
    string newContactPhone = txtContactPhone.Text;
    string newStatus = ddlStatus.SelectedValue;

    if (newForename != "" && newSurname != "" && newTermAddress != "" && newHomeAddress != "" && newPhone != "" && newDOB != "" && newContactName != "" && newContactAddress != "" && newContactPhone != "")
    {

        string student = Request.QueryString["user"];

        string connectionString = WebConfigurationManager.ConnectionStrings["scConnection"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();

                 string studentDetailsQuery = "UPDATE SisStudents SET TermAddress = @TermAddress, Phone = @Phone, DOB = @DOB, HomeAddress = @HomeAddress, Status = @Status WHERE UserID = @User";

        SqlCommand cmdStudentDetails = new SqlCommand(studentDetailsQuery, con);
        cmdStudentDetails.Parameters.AddWithValue("@TermAddress", newTermAddress);
        cmdStudentDetails.Parameters.AddWithValue("@Phone", newPhone);
        cmdStudentDetails.Parameters.AddWithValue("@DOB", newDOB);
        cmdStudentDetails.Parameters.AddWithValue("@HomeAddress", newHomeAddress);
        cmdStudentDetails.Parameters.AddWithValue("@User", student);
        cmdStudentDetails.Parameters.AddWithValue("@Status", newStatus);



        cmdStudentDetails.ExecuteNonQuery();

任何帮助都将受到赞赏,因为更新查询适用于其他所有内容。

编辑:没有错误,&#39;状态&#39;只是没有使用DropDownList中的选定值更新列。

1 个答案:

答案 0 :(得分:1)

如果只有状态没有得到更新,那么我想这可能是回发问题。

如果从页面加载中加载下拉列表,请确保在!Page.IsPostback内调用该函数,

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostback)
    {
        FillDropDownList();
    }
}