C# 根据下拉列表选择更新数据库?
我有一个名为SisStatus的表,它包含两列“StatusID”和“Status”,它包含四行,“Normal”、“dralled”、“Temporally dralled”和“Suspended” 我已将此表拉入下拉列表,并将其显示为以下代码:- ASP.NETC# 根据下拉列表选择更新数据库?,c#,asp.net,C#,Asp.net,我有一个名为SisStatus的表,它包含两列“StatusID”和“Status”,它包含四行,“Normal”、“dralled”、“Temporally dralled”和“Suspended” 我已将此表拉入下拉列表,并将其显示为以下代码:- ASP.NET <asp:DropDownList ID="ddlStatus" runat="server"> </asp:DropDownLi
<asp:DropDownList ID="ddlStatus" runat="server">
</asp:DropDownList>
这很好,正在填充下拉列表。当我试图更新另一个表“学生状态”列时,问题就出现了
下面是我用来尝试和更新它的代码,但是它不起作用。唯一可行的方法是,如果在数据库中(通过我已经将其放入),状态为“撤回”、“暂时撤回”、“暂停”,并且您将状态更改为“正常”,则它可以工作
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();
任何帮助都将不胜感激,因为更新查询适用于所有其他内容
编辑:没有错误,“状态”列只是没有使用DropDownList中的选定值进行更新。如果只有状态没有得到更新,那么我猜可能是回发的问题 如果您是从页面加载加载dropdownlist,请确保您正在调用
中的函数!Page.IsPostback
like
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
FillDropDownList();
}
}
我没有看到任何错误描述。您应该说明实际发生的错误。理想情况下使用调用堆栈转储。如果没有错误,则可能永远不会满足使用@User的查询中的where条件。它满足了,因为其他列正在用新值更新。。
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
FillDropDownList();
}
}