The following code is from a details page that receives a querystring from another page and then loads the details based on that id passed. That works fine, but now my problem is that the save changes button does not seem to be doing what it should be; the page just flashes like it submitted and loads all the values back, I’ve debugged and it all goes through fine, what could be the issue? Could it have something to do with me using querystring, because I have another update command on another page (no querystring) and it works perfectly…
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();
}
}
}
Your button click event handler
SaveCustomerChanges_Clickwill occur after yourPage_Loadevent handler. Your page load event handler is loading up your UI. You are updating the data after your UI has already loaded. You need to reload the UI after the update is performed ( think “DataBind” ).See the following msdn article for an overview on the ASP.NET Page Life Cycle:
http://msdn.microsoft.com/en-us/library/ms178472.aspx