protected void RadGrid1_OnItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridDataItem item = (GridDataItem) e.Item;
                string id = item["colId"].Text;
                switch (e.CommandName)
                {
                    case "btnSelect":
                        Response.Redirect("CustomerInfo.aspx?id=" + id, true);
                        break;
                    case "btnDelete":
                        int delete = new Customer().DeleteCustomerByCustomerId(int.Parse(id));
                        long addressid = GetAddressID(int.Parse(id));
                        int deleteAddress = new Addresses().DeleteAddressesByAddressId(addressid);
                        if (delete == 0)
                            Alert.Show("Data was not deleted");
                        else
                        {
                            this.LoadCustomerTable();
                        }
                        break;

                }
            }
            catch (Exception ex)
            {
                Alert.Show(ex.Message);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Customer objCustomer = new Customer();

            List<Customer> myList = objCustomer.GetAllCustomer(_company.CompanyId);

            objCustomer.CustomerId = myList.Count;
            objCustomer.CustomerName = txtCustomerName.Value;
            objCustomer.CustomerCategoryId =int.Parse(txtCustomerCategory.Value);
            objCustomer.SalesPersonId = int.Parse(salesPersonDropDownList.SelectedItem.Value);
            objCustomer.IsActive = chkIsActive.Checked;
            objCustomer.CompanyId = _company.CompanyId;
            objCustomer.CreditLimit = int.Parse(txtCreditLimit.Value);
            objCustomer.UpdateBy = _user.UserId;
            objCustomer.UpdateDate=DateTime.Now;

            Addresses address = new Addresses();
            List<Addresses> addresseses = address.GetAllAddresses(_company.CompanyId);
            address.AddressId = addresseses.Count;
            address.SourceType = "Customer";
            address.SourceId = _user.UserId;
            address.AddressType = "Main Address";
            address.AddressLine1 = txtAddressLine1.Value;
            address.AddressLine2 = txtAddressLine2.Value;
            address.CountryId = countryDropDownList.SelectedIndex;
            address.City = txtCity.Value;
            address.ZipCode = txtZipCode.Value;
            address.Phone = txtPhoneNo.Value;
            address.Mobile = txtPhoneNo.Value;
            address.Email = txtEmail.Value;
            address.Web = txtWeb.Value;
            address.CompanyId = _company.CompanyId;

            int chk1 = objCustomer.InsertCustomer();
            int chk2=address.InsertAddresses();
            if (chk1 > 0 && chk2 > 0)
                {
                    Session["savedCutomerMessage"] = "Saved Successfully";
                    Response.Redirect(Request.RawUrl);

                }
                else
                {
                    Alert.Show("Error occured while inserting customer information");
                }
        }
        private void LoadCustomerTable()
        {
            try
            {
                customerTblBody.InnerHtml = "";
                string htmlContent = "";
                Customer customer=new Customer();
                List<Customer> allCustomers = customer.GetAllCustomer(_company.CompanyId);
                foreach (Customer cust in allCustomers)
                {
                    htmlContent += "<tr>";
                    htmlContent += String.Format(@"<th>{0}</th><th>{1}</th><th>{2}</th><th>{3}</th><th>{4}</th>", cust.CustomerName, cust.CustomerCategoryId, cust.SalesPersonId,cust.CreditLimit, cust.UpdateDate);
                    htmlContent += "</tr>";
                }

                customerTblBody.InnerHtml += htmlContent;
            }
            catch (Exception exc)
            {
                Alert.Show(exc.Message);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Customer objCustomer = new Customer();

            objCustomer.CustomerName = txtCustomerName.Value;
            objCustomer.CustomerCategoryId = int.Parse(txtCustomerCategory.Value);
            objCustomer.SalesPersonId = int.Parse(salesPersonDropDownList.SelectedItem.Value);
            objCustomer.IsActive = chkIsActive.Checked;
            objCustomer.CompanyId = _company.CompanyId;
            objCustomer.CreditLimit = int.Parse(txtCreditLimit.Value);
            objCustomer.UpdateBy = _user.UserId;
            objCustomer.UpdateDate = DateTime.Now;

            Addresses address = new Addresses();

            address.SourceType = "Customer";

            address.AddressType = "Main Address";
            address.AddressLine1 = txtAddressLine1.Value;
            address.AddressLine2 = txtAddressLine2.Value;
            address.CountryId = countryDropDownList.SelectedIndex;
            address.City = txtCity.Value;
            address.ZipCode = txtZipCode.Value;
            address.Phone = txtPhoneNo.Value;
            address.Mobile = txtMobile.Value;
            address.Email = txtEmail.Value;
            address.Web = txtWeb.Value;
            address.CompanyId = _company.CompanyId;
            if ((lblId.Text == "" || lblId.Text == "0") && (addlblId.Text == "" || addlblId.Text == "0"))
            {
                objCustomer.CustomerId = new Customer().GetMaxCustomerID() + 1;
                address.SourceId = objCustomer.CustomerId;
                address.AddressId = new Addresses().GetMaxAddressId() + 1;
                int chk1 = objCustomer.InsertCustomer();
                int chk2 = address.InsertAddresses();
                if (chk1 > 0 && chk2 > 0)
                {

                    Session["savedCutomerMessage"] = "Saved Successfully";
                    Response.Redirect(Request.RawUrl);

                }
                else
                {
                    Alert.Show("Error occured while inserting customer information");
                }
            }
            else
            {
                address.AddressId =long.Parse( addlblId.Text);
                objCustomer.CustomerId = long.Parse(lblId.Text);
                int check = address.UpdateAddresses();
                check = objCustomer.UpdateCustomer();

                if (check > 0)
                {
                    Response.Redirect("CustomerList.aspx", true);
                }

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _company = (Company)Session["Company"];

            if (Session["savedCutomerMessage"] != null)
            {
                string msg = Session["savedCutomerMessage"].ToString();
                Alert.Show(msg);
                Session["savedCutomerMessage"] = null;
            }
            if (!isValidSession())
            {
                string str = Request.QueryString.ToString();
                if (str == string.Empty)
                    Response.Redirect("LogIn.aspx?refPage=index.aspx");
                else
                    Response.Redirect("LogIn.aspx?refPage=index.aspx?" + str);
            }

            this.LoadCountryDropdown();
            this.LoadSalesPersonDropDown();

            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    string customerID = Request.QueryString["id"].ToString();
                    Customer objCustomer = new Customer().GetCustomerByCustomerId(int.Parse(customerID), _company.CompanyId);
                    if (objCustomer != null || objCustomer.CustomerId != 0)
                    {
                        Addresses tempAddress = new Addresses();
                        List<Addresses> listAdddress = tempAddress.GetAllAddresses(_company.CompanyId);
                        foreach (Addresses addressese in listAdddress.Where(addressese => addressese.SourceId == objCustomer.CustomerId && addressese.SourceType == "Customer"))
                        {
                            tempAddress = addressese;
                            break;
                        }
                        addlblId.Text = tempAddress.AddressId.ToString();
                        lblId.Text = objCustomer.CustomerId.ToString();
                        txtCustomerName.Value = objCustomer.CustomerName;
                        txtCustomerCategory.Value = objCustomer.CustomerCategoryId.ToString();
                        SetIndex(salesPersonDropDownList, objCustomer.SalesPersonId.ToString());
                        countryDropDownList.SelectedIndex = tempAddress.CountryId;
                        chkIsActive.Checked = objCustomer.IsActive;
                        txtCreditLimit.Value = objCustomer.CreditLimit.ToString();
                        txtAddressLine1.Value = tempAddress.AddressLine1;
                        txtAddressLine2.Value = tempAddress.AddressLine2;
                        txtCity.Value = tempAddress.City;
                        txtZipCode.Value = tempAddress.ZipCode;
                        txtPhoneNo.Value = tempAddress.Phone;
                        txtMobile.Value = tempAddress.Mobile;
                        txtEmail.Value = tempAddress.Email;
                        txtWeb.Value = tempAddress.Web;
                    }
                }
            }
        }
        private void LoadCustomerTable()
        {
            try
            {
                Customer objCustomer = new Customer();
                objCustomerList = objCustomer.GetAllCustomer(_company.CompanyId);
                if (objCustomerList.Count == 0)
                {
                    objCustomerList.Add(new Customer());
                }
                foreach (Customer objCst in objCustomerList)
                {
                    List<string> countryList = Country.CountryList();
                    List<Addresses> addressList =new Addresses().GetAllAddresses(_company.CompanyId);
                    foreach (Addresses address in addressList)
                    {
                        if (address.SourceType == "Customer" && address.SourceId == objCst.CustomerId)
                        {
                            objCst.AddressId = address.AddressId;
                            objCst.AddressLine1 = address.AddressLine1;
                            objCst.AddressLine2 = address.AddressLine2;
                            objCst.AddressType = address.AddressType;
                            objCst.City = address.City;
                            objCst.ZipCode = address.ZipCode;
                            objCst.Phone = address.Phone;
                            objCst.Mobile = address.Mobile;
                            objCst.Email = address.Email;
                            objCst.Web = address.Web;
                            objCst.CountryName = countryList[address.CountryId];

                            break;
                        }

                    }

                }
                RadGrid1.DataSource = objCustomerList;
                RadGrid1.Rebind();

            }
            catch (Exception ex)
            {

                Alert.Show(ex.Message);

            }
        }