Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

just I wan to ask how to improve my code especially contact info block

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Compudata_ProjectManager.CodeFile.BOL;
using System.Web.Security;

namespace Compudata_ProjectManager
{
    public partial class NeCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // return early - nothing to do
            if (Page.IsPostBack)
            {
                return;
            }

            // redirect early - no need to continue
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx");
            }

            //get logged in user id UserID from MembershipUser
            MembershipUser memberUser = Membership.GetUser();
            string StrUserID = memberUser.ProviderUserKey.ToString();
            Guid userID = Guid.Parse(StrUserID);



            //hf_userID.Value = userID.ToString();
            if ( HideGridViewsIFCIstomerIsNew())
            {
                int customerID = int.Parse(Request.QueryString["customerID"]);
                Customer cust = Customer.GetCustomerDetail(customerID);

                PopulateControls(cust);
                p_contactsHTML.Visible = false;
                p_locationHTML.Visible = false;

            }
        }
        /// <summary>
        /// this Function will hide the gv_Contacts &  gv_location if Custoemr Has Contact and work location
        /// </summary>
        /// <returns></returns>
        public bool HideGridViewsIFCIstomerIsNew()
        {
            return (gv_Contacts.Rows.Count > 0 && gv_location.Rows.Count > 0);
        }


        /// <summary>
        /// PopulateControls
        /// </summary>
        /// <param name="cust"></param>
        private void PopulateControls(Customer cust)
        {
            ddl_title.SelectedValue = cust.Title;
            txtb_firstName.Text = cust.FirstName;
            txtb_lastName.Text = cust.LastName;
            txtb_postion.Text = cust.Postion;
            ddl_gender.SelectedValue = cust.Gender.ToString();
            ddl_company.SelectedValue = cust.CompanyID.ToString();
            btn_add.Visible = false;
        }


        /// <summary>
        /// Add new Contact to customer table 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                Customer customer = new Customer();
                customer.Title = ddl_title.SelectedValue.ToString();
                customer.FirstName = txtb_firstName.Text;
                customer.LastName = txtb_lastName.Text;
                customer.Postion = txtb_postion.Text;
                customer.Gender = Convert.ToChar(ddl_gender.SelectedValue.ToString());
                customer.CompanyID = Convert.ToInt32(ddl_company.SelectedValue.ToString());

                //Contact Info
                List<Contacts> contactsList = new List<Contacts> 
                { 
                    new Contacts { 
                        ContactDetail  = txtb_contact1.Text
                        ,LabelContactTypeID = Convert.ToInt32(hf_officephone.Value)
                        ,Status = chb_active_Contact1.Checked
                        ,Notes = txtb_noteContact1.Text},
                   new Contacts { 
                        ContactDetail  = txtb_contact2.Text
                        ,LabelContactTypeID = Convert.ToInt32(hf_cell.Value)
                        ,Status = chb_active_Contact1.Checked
                        ,Notes = txtb_noteContact2.Text},
                   new Contacts { 
                        ContactDetail  = txtb_contact3.Text
                        ,LabelContactTypeID = Convert.ToInt32(hf_email.Value)
                        ,Status = chb_active_Contact1.Checked
                        ,Notes = txtb_noteContact3.Text},
                    new Contacts { 
                        ContactDetail  = txtb_contact4.Text
                        ,LabelContactTypeID = Convert.ToInt32(hf_other.Value)
                        ,Status = chb_active_Contact1.Checked
                        ,Notes = txtb_noteContact4.Text}

                };

                //customer contact inforation
                Location local = new Location();
                local.Address = txtb_address.Text;
                local.City = Convert.ToString(ddl_city.SelectedValue);
                local.Province = Convert.ToString(ddl_provinces.SelectedValue);
                local.PostalCode = txtb_postalCode.Text;
                local.Note = noteLocation1.Value;


                bool successedAddCustomer_Contacts = Customer.AddNewCustomer_Contact(customer, contactsList, local);

                if (!successedAddCustomer_Contacts)
                {

                    lb_msg.Text ="contact added";

                    Response.Redirect("~/CustomersManagement/NeCustomer.aspx");
                }

                else
                    lb_msg.Text = "Can't add new customer";

            }

            catch
            { }

        }

        //adding more customer contact
        protected void gv_imgbtn_AddContact_Click(object sender, ImageClickEventArgs e)
        {

            Contacts contact = new Contacts();
            DropDownList gv_ddl_ContactType = (DropDownList)gv_Contacts.FooterRow.FindControl("gv_ddl_ContactType");
            TextBox gv_txtb_contactDetail = (TextBox)gv_Contacts.FooterRow.FindControl("gv_txtb_contactDetail");
            CheckBox gv_ChkB_Active = (CheckBox)gv_Contacts.FooterRow.FindControl("gv_ChkB_Active");
            TextBox gv_txtb_notes = (TextBox)gv_Contacts.FooterRow.FindControl("gv_txtb_notes");




            contact.CustomerID = Convert.ToInt32(Request.QueryString["customerID"]);
            contact.ContactDetail = gv_txtb_contactDetail.Text;
            contact.LabelContactTypeID = Convert.ToInt32(gv_ddl_ContactType.SelectedValue.ToString());
            contact.Status = gv_ChkB_Active.Checked;
            contact.Notes = gv_txtb_notes.Text;



            try
            {
                bool successedAddCustomer_Contacts = Contacts.AddNewCustomer_Contacts(contact);
                if (!successedAddCustomer_Contacts)
                {

                    var customerID = Convert.ToInt32(Request.QueryString["customerID"]);
                    Response.Redirect("~/NeCustomer.aspx?companyID=" + customerID.ToString());
                }

                else
                   lb_msg.Text = "Can't add new customer";

            }

            catch
            {
                lb_msg.Text = "Can't add new customer";
            }

        }

        // adding more location to customer
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            //location object 
            TextBox gv_txtb_address = (TextBox)gv_location.FooterRow.FindControl("gv_txtb_address");
            DropDownList gv_ddl_city = (DropDownList)gv_location.FooterRow.FindControl("gv_ddl_city");
            DropDownList gv_ddl_province = (DropDownList)gv_location.FooterRow.FindControl("gv_ddl_province");
            TextBox gv_txtb_postalCode = (TextBox)gv_location.FooterRow.FindControl("gv_txtb_postalCode");
            TextBox gv_txtb_note = (TextBox)gv_location.FooterRow.FindControl("gv_txtb_note");

            Location local = new Location();
            local.CustomerID = Convert.ToInt32(Request.QueryString["customerID"]);
            local.Address = gv_txtb_address.Text;
            local.City = gv_ddl_city.SelectedValue.ToString();
            local.Province = gv_ddl_province.SelectedValue.ToString();
            local.PostalCode = gv_txtb_postalCode.Text;
            local.Note = gv_txtb_note.Text;

            try
            {
                bool successedAddCustomer_Contacts = Location.Add_Customer_Locations(local);
                if (!successedAddCustomer_Contacts)
                {

                    var customerID = Convert.ToInt32(Request.QueryString["customerID"]);
                    Response.Redirect("~/NeCustomer.aspx?customerID=" + customerID.ToString());
                }

                else
                    lb_msg.Text = "Can't add new customer";

            }

            catch
            {
                lb_msg.Text = "Can't add new customer";
            }
        }



    }
}
share|improve this question
    
You can use the Model View Presenter pattern to abstract away the GUI - head over to pluralsight-training.net –  Shelakel Feb 10 '13 at 20:09
add comment

1 Answer

up vote 1 down vote accepted

A couple minor changes I would make in the loading. I didn't review the rest. I am assuming (probably correctly) that memberUser.ProviderUserKey is always a Guid at runtime.

        //get logged in user id UserID from MembershipUser
        MembershipUser memberUser = Membership.GetUser();

        // redirect early - no need to continue
        if (memberUser == null)
        {
            Response.Redirect("~/Account/Login.aspx");
        }

        Guid userID = (Guid) memberUser.ProviderUserKey;

You may want to use TryParse for Request.QueryString["customerID"], as the QueryString is user input which may not be valid.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.