Saving class properties across several events?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sodapop
    New Member
    • Jun 2010
    • 2

    Saving class properties across several events?

    Hi, I am currently coding a web form that takes an address and verifies it with the United States Postal Service before inserting the record into a MS Access Database. Along with this, I have a checkbox (with AutoPostBack) that verifies and saves the content in the text boxes as billing information in a customer class before clearing the text fields to enter in the shipping info (newCustomer.sF irstName property for shipping vs. newCustomer.fir stName for billing & shipping). My problem lies when I click the checkbox to verify and 'save' the billing address and re-enter the address. The data for the first address gets set to null and I get an error. (cannot enter null into database)

    What can I do to prevent (or enable) to properly set the data to the customer class properties without getting wiped across events? Here is my code thus far:

    Code:
    protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Customer existCust = new Customer();
    
            blowinDT = new dsSMOOTH.SG_Sub_Invoice_CardsDataTable();
            blowinTA = new dsSMOOTHTableAdapters.SG_Sub_Invoice_CardsTableAdapter();
            blowinDT = blowinTA.GetData();
    
            MAX.USPS.USPSManager m = new MAX.USPS.USPSManager("726STARM2333", true);
            MAX.USPS.Address a = new MAX.USPS.Address();
            a.Address2 = txtAddress.Text;
            a.City = txtCity.Text;
            a.State = txtState.Text;
            MAX.USPS.Address validatedAddress = m.ValidateAddress(a);
            if (validatedAddress == null)
            {
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Text = "The address entered was invalid";
            }
            else
            {
                if (chkSameAdd.Checked == true)
                {
                    newCust.SInstID = txtInstID.Text;
                    newCust.SFirstName = txtFName.Text;
                    newCust.SLastName = txtLName.Text;
                    newCust.SMiddleInitial = txtMiddleInitial.Text;
                    newCust.SAddress = validatedAddress.Address2;
                    newCust.Address2 = txtAddress2.Text;
                    newCust.SCity = validatedAddress.City;
                    newCust.SState = validatedAddress.State;
                    newCust.SZip = Convert.ToDouble(validatedAddress.Zip);
                    newCust.Insert(newCust);
    
                    lblError.ForeColor = System.Drawing.Color.Green;
                    if (newCust.SFirstName != null)
                        lblError.Text = "Address Information for " + newCust.FirstName + ", " + newCust.LastName + " and " + newCust.SFirstName + ", " + newCust.SLastName + " has been validated and saved to the database! Ready for next entry.";
    
                    txtFName.Text = "";
                    txtLName.Text = "";
                    txtInstID.Text = "";
                    txtMiddleInitial.Text = "";
                    txtAddress.Text = "";
                    txtAddress2.Text = "";
                    txtCity.Text = "";
                    txtState.Text = "";
                    txtZip.Text = "";
                    txtPhone.Text = "";
                    txtEmail.Text = "";
                    txtBlowIn.Text = "";
                }
                else
                {
                    newCust.FirstName = txtFName.Text;
                    newCust.LastName = txtLName.Text;
                    newCust.Address = validatedAddress.Address2;
                    newCust.City = validatedAddress.City;
                    newCust.State = validatedAddress.State;
                    newCust.Zip = validatedAddress.Zip + "-" + validatedAddress.ZipPlus4;
                        newCust.InstID = txtInstID.Text;
                        newCust.Address2 = txtAddress2.Text;
                        newCust.BlowInIssue = txtBlowIn.Text;
                        newCust.Email = txtEmail.Text;
                        newCust.MiddleInitial = txtMiddleInitial.Text;
                        double phone = Convert.ToDouble(txtPhone.Text);
                        newCust.Phone = String.Format("{0:(###)###-####}", phone);
                        if (chkSameAdd.Checked == false)
                        {
                            newCust.CloneAddress = true;
                            newCust.SFirstName = "";
                            newCust.SLastName = "";
                            newCust.SMiddleInitial = "";
                            newCust.SInstID = "";
                            newCust.SAddress = "";
                            newCust.SAddress2 = "";
                            newCust.SCity = "";
                            newCust.SState = "";
                        }
                        newCust.Insert(newCust);
    
                        lblError.ForeColor = System.Drawing.Color.Green;
                        if (newCust.SFirstName != null)
                            lblError.Text = "Address information for " + newCust.LastName + ", " + newCust.FirstName + " has been validated and saved to the database! Ready for next entry.";
    
                        txtFName.Text = "";
                        txtLName.Text = "";
                        txtInstID.Text = "";
                        txtMiddleInitial.Text = "";
                        txtAddress.Text = "";
                        txtAddress2.Text = "";
                        txtCity.Text = "";
                        txtState.Text = "";
                        txtZip.Text = "";
                        txtPhone.Text = "";
                        txtEmail.Text = "";
                        txtBlowIn.Text = "";
                    }
                }
            }
        
        protected void chkSameAdd_CheckedChanged(object sender, EventArgs e)
        {
            if (txtFName.Text != string.Empty && txtLName.Text != string.Empty && txtAddress.Text != string.Empty && txtCity.Text != string.Empty && txtState.Text != string.Empty && txtZip.Text != string.Empty)
            {
                Customer existCust = new Customer();
    
                blowinDT = new dsSMOOTH.SG_Sub_Invoice_CardsDataTable();
                blowinTA = new dsSMOOTHTableAdapters.SG_Sub_Invoice_CardsTableAdapter();
                blowinDT = blowinTA.GetData();
    
                MAX.USPS.USPSManager m = new MAX.USPS.USPSManager("726STARM2333", true);
                MAX.USPS.Address a = new MAX.USPS.Address();
                a.Address2 = txtAddress.Text;
                a.City = txtCity.Text;
                a.State = txtState.Text;
                MAX.USPS.Address validatedAddress = m.ValidateAddress(a);
                if (validatedAddress == null)
                {
                    lblError.ForeColor = System.Drawing.Color.Red;
                    lblError.Text = "The address entered was invalid";
                }
                else
                {
                    newCust.FirstName = txtFName.Text;
                    newCust.LastName = txtLName.Text;
                    newCust.MiddleInitial = txtMiddleInitial.Text;
                    newCust.Address = validatedAddress.Address2;
                    newCust.City = validatedAddress.City;
                    newCust.State = validatedAddress.State;
                    newCust.Zip = validatedAddress.Zip;
                    newCust.Email = txtEmail.Text;
                    double phone = Convert.ToDouble(txtPhone.Text);
                    newCust.Phone = String.Format("{0:(###)###-####}", phone);
                    newCust.CloneAddress = true;
                    newCust.Address2 = txtAddress2.Text;
    
                        lblError.ForeColor = System.Drawing.Color.Green;
                        lblError.Text = "Billing information for " + newCust.FirstName + " " + newCust.LastName + " has been saved. Please enter the shipping information now.";
                        txtFName.Text = "";
                        txtLName.Text = "";
                        txtInstID.Text = "";
                        txtMiddleInitial.Text = "";
                        txtAddress.Text = "";
                        txtAddress2.Text = "";
                        txtCity.Text = "";
                        txtState.Text = "";
                        txtZip.Text = "";
                        txtPhone.Text = "";
                        txtEmail.Text = "";
                    
                }
            }
            else
            {
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Text = "Please fill out the necessary fields";
                chkSameAdd.Checked = false;
            }
        }
    }
    Sorry for the sloppy coding! I'm an amateur, If anyone has any suggestions on how to clean it up, I'm all open!
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by Sodapop
    Hi, I am currently coding a web form that takes an address and verifies it with the United States Postal Service before inserting the record into a MS Access Database. Along with this, I have a checkbox (with AutoPostBack) that verifies and saves the content in the text boxes as billing information in a customer class before clearing the text fields to enter in the shipping info (newCustomer.sF irstName property for shipping vs. newCustomer.fir stName for billing & shipping). My problem lies when I click the checkbox to verify and 'save' the billing address and re-enter the address. The data for the first address gets set to null and I get an error. (cannot enter null into database)

    What can I do to prevent (or enable) to properly set the data to the customer class properties without getting wiped across events? Here is my code thus far:

    Code:
    protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Customer existCust = new Customer();
    
            blowinDT = new dsSMOOTH.SG_Sub_Invoice_CardsDataTable();
            blowinTA = new dsSMOOTHTableAdapters.SG_Sub_Invoice_CardsTableAdapter();
            blowinDT = blowinTA.GetData();
    
            MAX.USPS.USPSManager m = new MAX.USPS.USPSManager("726STARM2333", true);
            MAX.USPS.Address a = new MAX.USPS.Address();
            a.Address2 = txtAddress.Text;
            a.City = txtCity.Text;
            a.State = txtState.Text;
            MAX.USPS.Address validatedAddress = m.ValidateAddress(a);
            if (validatedAddress == null)
            {
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Text = "The address entered was invalid";
            }
            else
            {
                if (chkSameAdd.Checked == true)
                {
                    newCust.SInstID = txtInstID.Text;
                    newCust.SFirstName = txtFName.Text;
                    newCust.SLastName = txtLName.Text;
                    newCust.SMiddleInitial = txtMiddleInitial.Text;
                    newCust.SAddress = validatedAddress.Address2;
                    newCust.Address2 = txtAddress2.Text;
                    newCust.SCity = validatedAddress.City;
                    newCust.SState = validatedAddress.State;
                    newCust.SZip = Convert.ToDouble(validatedAddress.Zip);
                    newCust.Insert(newCust);
    
                    lblError.ForeColor = System.Drawing.Color.Green;
                    if (newCust.SFirstName != null)
                        lblError.Text = "Address Information for " + newCust.FirstName + ", " + newCust.LastName + " and " + newCust.SFirstName + ", " + newCust.SLastName + " has been validated and saved to the database! Ready for next entry.";
    
                    txtFName.Text = "";
                    txtLName.Text = "";
                    txtInstID.Text = "";
                    txtMiddleInitial.Text = "";
                    txtAddress.Text = "";
                    txtAddress2.Text = "";
                    txtCity.Text = "";
                    txtState.Text = "";
                    txtZip.Text = "";
                    txtPhone.Text = "";
                    txtEmail.Text = "";
                    txtBlowIn.Text = "";
                }
                else
                {
                    newCust.FirstName = txtFName.Text;
                    newCust.LastName = txtLName.Text;
                    newCust.Address = validatedAddress.Address2;
                    newCust.City = validatedAddress.City;
                    newCust.State = validatedAddress.State;
                    newCust.Zip = validatedAddress.Zip + "-" + validatedAddress.ZipPlus4;
                        newCust.InstID = txtInstID.Text;
                        newCust.Address2 = txtAddress2.Text;
                        newCust.BlowInIssue = txtBlowIn.Text;
                        newCust.Email = txtEmail.Text;
                        newCust.MiddleInitial = txtMiddleInitial.Text;
                        double phone = Convert.ToDouble(txtPhone.Text);
                        newCust.Phone = String.Format("{0:(###)###-####}", phone);
                        if (chkSameAdd.Checked == false)
                        {
                            newCust.CloneAddress = true;
                            newCust.SFirstName = "";
                            newCust.SLastName = "";
                            newCust.SMiddleInitial = "";
                            newCust.SInstID = "";
                            newCust.SAddress = "";
                            newCust.SAddress2 = "";
                            newCust.SCity = "";
                            newCust.SState = "";
                        }
                        newCust.Insert(newCust);
    
                        lblError.ForeColor = System.Drawing.Color.Green;
                        if (newCust.SFirstName != null)
                            lblError.Text = "Address information for " + newCust.LastName + ", " + newCust.FirstName + " has been validated and saved to the database! Ready for next entry.";
    
                        txtFName.Text = "";
                        txtLName.Text = "";
                        txtInstID.Text = "";
                        txtMiddleInitial.Text = "";
                        txtAddress.Text = "";
                        txtAddress2.Text = "";
                        txtCity.Text = "";
                        txtState.Text = "";
                        txtZip.Text = "";
                        txtPhone.Text = "";
                        txtEmail.Text = "";
                        txtBlowIn.Text = "";
                    }
                }
            }
        
        protected void chkSameAdd_CheckedChanged(object sender, EventArgs e)
        {
            if (txtFName.Text != string.Empty && txtLName.Text != string.Empty && txtAddress.Text != string.Empty && txtCity.Text != string.Empty && txtState.Text != string.Empty && txtZip.Text != string.Empty)
            {
                Customer existCust = new Customer();
    
                blowinDT = new dsSMOOTH.SG_Sub_Invoice_CardsDataTable();
                blowinTA = new dsSMOOTHTableAdapters.SG_Sub_Invoice_CardsTableAdapter();
                blowinDT = blowinTA.GetData();
    
                MAX.USPS.USPSManager m = new MAX.USPS.USPSManager("726STARM2333", true);
                MAX.USPS.Address a = new MAX.USPS.Address();
                a.Address2 = txtAddress.Text;
                a.City = txtCity.Text;
                a.State = txtState.Text;
                MAX.USPS.Address validatedAddress = m.ValidateAddress(a);
                if (validatedAddress == null)
                {
                    lblError.ForeColor = System.Drawing.Color.Red;
                    lblError.Text = "The address entered was invalid";
                }
                else
                {
                    newCust.FirstName = txtFName.Text;
                    newCust.LastName = txtLName.Text;
                    newCust.MiddleInitial = txtMiddleInitial.Text;
                    newCust.Address = validatedAddress.Address2;
                    newCust.City = validatedAddress.City;
                    newCust.State = validatedAddress.State;
                    newCust.Zip = validatedAddress.Zip;
                    newCust.Email = txtEmail.Text;
                    double phone = Convert.ToDouble(txtPhone.Text);
                    newCust.Phone = String.Format("{0:(###)###-####}", phone);
                    newCust.CloneAddress = true;
                    newCust.Address2 = txtAddress2.Text;
    
                        lblError.ForeColor = System.Drawing.Color.Green;
                        lblError.Text = "Billing information for " + newCust.FirstName + " " + newCust.LastName + " has been saved. Please enter the shipping information now.";
                        txtFName.Text = "";
                        txtLName.Text = "";
                        txtInstID.Text = "";
                        txtMiddleInitial.Text = "";
                        txtAddress.Text = "";
                        txtAddress2.Text = "";
                        txtCity.Text = "";
                        txtState.Text = "";
                        txtZip.Text = "";
                        txtPhone.Text = "";
                        txtEmail.Text = "";
                    
                }
            }
            else
            {
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Text = "Please fill out the necessary fields";
                chkSameAdd.Checked = false;
            }
        }
    }
    Sorry for the sloppy coding! I'm an amateur, If anyone has any suggestions on how to clean it up, I'm all open!
    Yeah... that code looks real sloppy...

    Have you set the ViewState property of the textbox controls to True..

    Comment

    • Sodapop
      New Member
      • Jun 2010
      • 2

      #3
      view state's are all set to true. Working on cleaning up the code

      Comment

      Working...