sql表在按钮单击时不更新

时间:2012-06-29 17:00:00

标签: c# asp.net sql web-applications

以下代码来自详细信息页面,该页面从另一个页面接收查询字符串,然后根据传递的ID加载详细信息。这工作正常,但现在我的问题是保存更改按钮似乎没有做它应该是什么;页面只是像它提交的那样闪烁并加载所有的值,我已经调试了,这一切都很顺利,可能是什么问题?它可能与我使用查询字符串有关,因为我在另一个页面上有另一个更新命令(没有查询字符串),它完美地运行...

    namespace CC
{
    public partial class customerdetails : System.Web.UI.Page
    {
        int customerID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            customerID = Convert.ToInt32(Request.QueryString["id"]);
            string strConnString2 = "Data Source=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            using (SqlConnection con = new SqlConnection(strConnString2))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SELECT CustomerStatus, CustomerGroup, CustomerFirstName, CustomerLastName, CompanyName, CustomerAddress, CustomerCity, CustomerState, CustomerZipcode, CustomerEmail, CustomerEmail2, CustomerPhoneNumber, CustomerPhoneNumberExt, CustomerType, CustomerSubscriptionType, CustomerCost, CustomerPaymentMethod, CustomerSignUpDate, CustomerPickUpDay, CustomerPickUpDay2, CustomerNotes FROM Customers WHERE CustomerID = @CustomerID";
                    cmd.Parameters.AddWithValue("@CustomerID", customerID);
                    con.Open();
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (reader["CustomerStatus"].ToString() == "New")
                            {
                                StatusLabel.ForeColor = System.Drawing.Color.YellowGreen;
                            }
                            else
                            {
                                if (reader["CustomerStatus"].ToString() == "Suspended")
                                {
                                    StatusLabel.ForeColor = System.Drawing.Color.Aqua;
                                }
                                else
                                {
                                    StatusLabel.ForeColor = System.Drawing.Color.DarkRed;
                                }
                            }
                            StatusLabel.Text = reader["CustomerStatus"].ToString();
                            if (reader["CustomerGroup"].ToString() == "")
                            {
                            }
                            else
                            {
                                GroupDropDown.SelectedValue = reader["CustomerGroup"].ToString();
                            }
                            txtCustomerFirstName.Text = reader["CustomerFirstName"].ToString();
                            txtCustomerLastName.Text = reader["CustomerLastName"].ToString();
                            txtCompanyName.Text = reader["CompanyName"].ToString();
                            txtCustomerAddress.Text = reader["CustomerAddress"].ToString();
                            txtCustomerCity.Text = reader["CustomerCity"].ToString();
                            txtCustomerState.Text = reader["CustomerState"].ToString();
                            txtCustomerZipcode.Text = reader["CustomerZipcode"].ToString();
                            txtCustomerEmail.Text = reader["CustomerEmail"].ToString();
                            if (reader["CustomerEmail2"].ToString() == "")
                            {

                            }
                            else
                            {
                                Email2CheckBox.Checked = true;
                                txtCustomerEmail2.Visible = true;
                                txtCustomerEmail2.Text = reader["CustomerEmail2"].ToString();
                            }
                            txtCustomerPhoneNumber.Text = reader["CustomerPhoneNumber"].ToString();
                            txtCustomerPhoneNumberExt.Text = reader["CustomerPhoneNumberExt"].ToString();
                            CustomerType.SelectedValue = reader["CustomerType"].ToString();
                            SubscriptionType.SelectedValue = reader["CustomerSubscriptionType"].ToString();
                            CostTxtBx.Text = reader["CustomerCost"].ToString();
                            CustomerPaymentMethod.SelectedValue = reader["CustomerPaymentMethod"].ToString();
                            CustomerSignUpDate.Text = reader["CustomerSignUpDate"].ToString();
                            PickUpDay.SelectedValue = reader["CustomerPickUpDay"].ToString();
                            if (reader["CustomerPickUpDay2"].ToString() == "")
                            {

                            }
                            else
                            {
                                PickUpDay2CheckBox.Checked = true;
                                PickUpDay2.Visible = true;
                                PickUpDay2.SelectedValue = reader["CustomerPickUpDay2"].ToString();
                            }
                            if (reader["CustomerNotes"].ToString() == "")
                            {
                            }
                            else
                            {
                                string sqlfriendlyNotes = reader["CustomerNotes"].ToString().Replace("<br/>", "\n");

                                NotesTxtBx.Text = sqlfriendlyNotes;
                            }

                        }
                    }
                    con.Close();
                }
            }
        }

        protected void SaveCustomerChanges_Click(object sender, EventArgs e)
        {
            string Status = this.StatusLabel.Text;
            string Group = this.GroupDropDown.Text;
            string customerStatus = Status;
            string customerFirstName = this.txtCustomerFirstName.Text;
            string customerLastName = this.txtCustomerLastName.Text;
            string customerGroup = Group;
            string companyName = this.txtCompanyName.Text;
            string customerAddress = this.txtCustomerAddress.Text;
            string customerCity = this.txtCustomerCity.Text;
            string customerState = this.txtCustomerState.Text;
            string customerZipcode = this.txtCustomerZipcode.Text;
            string customerEmail = this.txtCustomerEmail.Text;
            string customerEmail2 = this.txtCustomerEmail2.Text;
            string customerPhoneNumber = this.txtCustomerPhoneNumber.Text;
            string customerPhoneNumberExt = this.txtCustomerPhoneNumberExt.Text;
            string customerType = this.CustomerType.Text;
            string customerSubscriptionType = this.SubscriptionType.Text;
            string customerCost = this.CostTxtBx.Text;
            string customerPaymentMethod = this.CustomerPaymentMethod.Text;
            string customerSignUpDate = this.CustomerSignUpDate.Text;
            string customerPickUpDay = this.PickUpDay.Text;
            string customerPickUpDay2 = this.PickUpDay2.Text;
            string customerNotes = this.NotesTxtBx.Text.Replace("\n", "<br/>");
            string strConnString1 = "Data Source=xxxxxxxxxxxxxx";
            using (SqlConnection con = new SqlConnection(strConnString1))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "UPDATE Customers SET CustomerStatus=@CustomerStatus, CustomerGroup=@CustomerGroup, CustomerFirstName=@CustomerFirstName, CustomerLastName=@CustomerLastName, CompanyName=@CompanyName, CustomerAddress=@CustomerAddress, CustomerCity=@CustomerCity, CustomerState=@CustomerState, CustomerZipcode=@CustomerZipcode, CustomerEmail=@CustomerEmail, CustomerEmail2=@CustomerEmail2, CustomerPhoneNumber=@CustomerPhoneNumber, CustomerPhoneNumberExt=@CustomerPhoneNumberExt, CustomerType=@CustomerType, CustomerSubscriptionType=@CustomerSubscriptionType, CustomerCost=@CustomerCost, CustomerPaymentMethod=@CustomerPaymentMethod, CustomerSignUpDate=@CustomerSignUpDate, CustomerPickUpDay=@CustomerPickUpDay, CustomerPickUpDay2=@CustomerPickUpDay2, CustomerNotes=@CustomerNotes WHERE CustomerID = @CustomerID";
                    cmd.Parameters.AddWithValue("@CustomerID", customerID);
                    cmd.Parameters.AddWithValue("@CustomerStatus", customerStatus);
                    cmd.Parameters.AddWithValue("@CustomerGroup", customerGroup);
                    cmd.Parameters.AddWithValue("@CustomerFirstName", customerFirstName);
                    cmd.Parameters.AddWithValue("@CustomerLastName", customerLastName);
                    cmd.Parameters.AddWithValue("@CompanyName", companyName);
                    cmd.Parameters.AddWithValue("@CustomerAddress", customerAddress);
                    cmd.Parameters.AddWithValue("@CustomerCity", customerCity);
                    cmd.Parameters.AddWithValue("@CustomerState", customerState);
                    cmd.Parameters.AddWithValue("@CustomerZipcode", customerZipcode);
                    cmd.Parameters.AddWithValue("@CustomerEmail", customerEmail);
                    cmd.Parameters.AddWithValue("@CustomerEmail2", customerEmail2);
                    cmd.Parameters.AddWithValue("@CustomerPhoneNumber", customerPhoneNumber);
                    cmd.Parameters.AddWithValue("@CustomerPhoneNumberExt", customerPhoneNumberExt);
                    cmd.Parameters.AddWithValue("@CustomerType", customerType);
                    cmd.Parameters.AddWithValue("@CustomerSubscriptionType", customerSubscriptionType);
                    cmd.Parameters.AddWithValue("@CustomerCost", customerCost);
                    cmd.Parameters.AddWithValue("@CustomerPaymentMethod", customerPaymentMethod);
                    cmd.Parameters.AddWithValue("@CustomerSignUpDate", customerSignUpDate);
                    cmd.Parameters.AddWithValue("@CustomerPickUpDay", customerPickUpDay);
                    cmd.Parameters.AddWithValue("@CustomerPickUpDay2", customerPickUpDay2);
                    cmd.Parameters.AddWithValue("@CustomerNotes", customerNotes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

您的按钮点击事件处理程序SaveCustomerChanges_Click将在您的Page_Load事件处理程序之后发生。您的页面加载事件处理程序正在加载您的UI。在UI已加载后,您正在更新数据。您需要在执行更新后重新加载UI(想想“DataBind”)。

有关ASP.NET页面生命周期的概述,请参阅以下msdn文章: http://msdn.microsoft.com/en-us/library/ms178472.aspx