protected void txtCustomerName_TextChanged(object sender, EventArgs e)
 {
     if (txtCustomerName.Text.Count() >= 3)
     {
         GridCustomers.Visible = true;
         string srch = txtCustomerName.Text;
         LINQ_CustDataContext db = new LINQ_CustDataContext();
         var cst = (from g in db.Customers
                    where g.FName.Contains(srch) || g.LName.Contains(srch) || g.Company.Contains(srch)
                    select new { g.id, g.Company, g.FName, g.LName, g.City});
         GridCustomers.DataSource = cst;
         GridCustomers.DataBind();
     }
 }
 private void GetRecentActivity(int cid)
 {
     try
     {
         LINQ_CustDataContext db = new LINQ_CustDataContext();
         var rslts = db.GetRecentCustomerActivity(cid).ToList();
         GridRecentActivity.DataSource = rslts;
         GridRecentActivity.DataBind();
     }
     catch (Exception ex)
     {
         Database.WriteException("private void GetRecentActivity(int cid)",Page, HttpContext.Current, ex );
     }
 }
    protected void GridCustomers_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int idd = Convert.ToInt32(e.Item.Cells[1].Text);
        if (e.CommandName == "select_customer")
        {
            GridCustomers.Visible = false;

            LINQ_CustDataContext db = new LINQ_CustDataContext();
            var cst = (from g in db.Customers
                      where g.id == idd
                      select g).FirstOrDefault();
            lblCompany.Text = cst.Company;
            lblCustomerName.Text = string.Format("{0} {1}", cst.FName, cst.LName);
            lblAddress.Text = string.Format("{0} {1}", cst.Address, cst.Address2);
            lblAddress2.Text = string.Format("{0}, {1} {2}", cst.City, cst.State, cst.Zip);
            lblPhone.Text = Variables.PhoneNumberFormat(Convert.ToInt64(cst.Phone));
            lblPhone2.Text = Variables.PhoneNumberFormat(Convert.ToInt64(cst.Phone2));
            lblEmail.Text = cst.Email;
        }
        GetRecentActivity(idd);
    }
    protected void btnSaveEnt_Click(object sender, EventArgs e)
    {
        //bool isnew = false;
        bool iscustomer = false;
        //if (Request.QueryString["isnew"] == "1") { isnew = true; }
        if (thisUserType == UserType.AccountCustomer) { iscustomer = true; }
        if (isNew && (hiddenEntityId.Value == ""))
        {
            LINQ_CustDataContext dc = new LINQ_CustDataContext();
           if (iscustomer)
           {
               //Save entity as customer
               Customer cst = new Customer();
               cst.FName = txtFName.Text;
               cst.LName = txtLName.Text;
               cst.Account = Users.GetAccountNumber(myUserId);
               cst.Address = txtAddress.Text;
               cst.Address2 = txtAddress2.Text;
               cst.City = txtCity.Text;
               cst.State = ddState.SelectedValue;
               cst.Zip = Convert.ToInt32(txtZip.Text);
               cst.Email = txtEmail.Text;
               cst.Phone = Variables.PhoneNumberFormat(txtPhone.Text);
               cst.Phone2 = Variables.PhoneNumberFormat(txtPhone2.Text);
               cst.Company = txtCompany.Text;
               try
               {
                   dc.Customers.InsertOnSubmit(cst);
                   dc.SubmitChanges();
                   hiddenEntityId.Value = cst.id.ToString();
                   Master.SetStatus("Customer added",1);
                   hiddenEntityId.Value = cst.id.ToString();
                   btnSaveEnt.Visible = false;
                   btnEditEnt.Visible = true;
               }
               catch (Exception ex)
               {
                   Database.WriteException("Error adding customer. btnSaveEnt_Click", Page, HttpContext.Current, ex);
                   Master.SetStatus("Error adding customer", 2);
               }

           }
           else
           {
               //Save entity as customer
               Employee emp = new Employee();
                emp.FName = txtFName.Text;
                emp.LName = txtLName.Text;
               emp.Account = Users.GetAccountNumber(myUserId);
               emp.Address = txtAddress.Text;
                emp.Address2 = txtAddress2.Text;
                emp.City = txtCity.Text;
               emp.State = ddState.SelectedValue;
               emp.Zip = Convert.ToInt32(txtZip.Text);
               emp.Email = txtEmail.Text;
               emp.Phone = Variables.PhoneNumberFormat(txtPhone.Text);
               emp.Phone2 = Variables.PhoneNumberFormat(txtPhone2.Text);
               try
               {
                   dc.Employees.InsertOnSubmit(emp);
                   dc.SubmitChanges();
                   hiddenEntityId.Value = emp.id.ToString();
                   Master.SetStatus("Employee added", 1);
                   hiddenEntityId.Value = emp.id.ToString();
                   btnSaveEnt.Visible = false;
                   btnEditEnt.Visible = true;
               }
               catch (Exception ex)
               {
                   Database.WriteException("Error adding employee. btnSaveEnt_Click", Page, HttpContext.Current, ex);
                   Master.SetStatus("Error adding employee", 2);
               }
           }

        }
        else
        {

        try
        {
            if (thisUserType == UserType.AccountCustomer)
            {
            LINQ_CustDataContext dc = new LINQ_CustDataContext();
            var userInfo = (from a in dc.Customers
                            where a.id == Convert.ToInt32(hiddenEntityId.Value)
                            select a).FirstOrDefault();
            if (userInfo != null)
            {
                //set vals and update
                userInfo.FName = txtFName.Text;
                userInfo.LName = txtLName.Text;
                userInfo.Address = txtAddress.Text;
                userInfo.Address2 = txtAddress2.Text;
                userInfo.City = txtCity.Text;
                userInfo.State = ddState.SelectedValue;
                userInfo.Email = txtEmail.Text;
                UpdateEmail(txtEmail.Text);
                int zip = Convert.ToInt32(txtZip.Text);
                if (zip != 0) { userInfo.Zip = zip; }
                long ph1 = Variables.PhoneNumberFormat(txtPhone.Text);
                long ph2 = Variables.PhoneNumberFormat(txtPhone2.Text);
                if (ph1 != 0) { userInfo.Phone = ph1; }
                if (ph2 != 0) { userInfo.Phone2 = ph2; }

                dc.SubmitChanges();
                Master.SetStatus("Customer information updated",1);
                btnSaveEnt.Visible = false;
                btnEditEnt.Visible = true;
            }
            }
            else
            {
                LINQ_CustDataContext dc = new LINQ_CustDataContext();
                var userInfo = (from a in dc.Employees
                                where a.id == Convert.ToInt32(hiddenEntityId.Value)
                                select a).FirstOrDefault();
                if (userInfo != null)
                {
                    //set vals and update
                    userInfo.FName = txtFName.Text;
                    userInfo.LName = txtLName.Text;
                    userInfo.Address = txtAddress.Text;
                    userInfo.Address2 = txtAddress2.Text;
                    userInfo.City = txtCity.Text;
                    userInfo.State = ddState.SelectedValue;
                    userInfo.Email = txtEmail.Text;
                    UpdateEmail(txtEmail.Text);
                    int zip = Convert.ToInt32(txtZip.Text);
                    if (zip != 0) { userInfo.Zip = zip; }
                    long ph1 = Variables.PhoneNumberFormat(txtPhone.Text);
                    long ph2 = Variables.PhoneNumberFormat(txtPhone2.Text);
                    if (ph1 != 0) { userInfo.Phone = ph1; }
                    if (ph2 != 0) { userInfo.Phone2 = ph2; }

                    dc.SubmitChanges();
                    Master.SetStatus("Employee information updated", 1);
                    btnSaveEnt.Visible = false;
                    btnEditEnt.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {
            Database.WriteException("Error updating WebUser information. btnSave_Click", Page, HttpContext.Current, ex);
            Master.SetStatus("Error updating customer information", 2);
        }
        }
    }
    private void LoadUser(int webuserid,bool isEmployee)
    {
        LINQ_UsersDataContext dc = new LINQ_UsersDataContext();
        LINQ_CustDataContext cc = new LINQ_CustDataContext();
        var userInfoA = (from a in dc.WebAccounts
                        where a.id == webuserid
                        select a).FirstOrDefault();
        if (userInfoA != null)
        {
            //Values from WebAccounts table
            txtUserName.Text = userInfoA.UserName;
            hiddenUserId.Value = userInfoA.id.ToString();

        }

        if (isEmployee)
        {
            tblDetail.Rows[1].Visible = false;
            var userInfo = (from b in cc.Employees
                   where b.id == userInfoA.Employee
                   select b).FirstOrDefault();
            if (userInfo != null)
            {
                //Values from Employee table
                lblAccount.Text = Users.GetAccountName(webuserid);
                txtFName.Text = userInfo.FName;
                txtLName.Text = userInfo.LName;
                txtAddress.Text = userInfo.Address;
                txtAddress2.Text = userInfo.Address2;
                txtCity.Text = userInfo.City;
                ddState.SelectedValue = userInfo.State;
                txtZip.Text = userInfo.Zip.ToString();
                txtPhone.Text = Variables.PhoneNumberFormat(userInfo.Phone);
                txtPhone2.Text = Variables.PhoneNumberFormat(userInfo.Phone2);
                hiddenEntityId.Value = userInfo.id.ToString();
                txtEmail.Text = userInfo.Email;
            }
        }
        else
        {
            var userInfo = (from b in cc.Customers
                                       where b.id == userInfoA.Customer
                                       select b).FirstOrDefault();
            if (userInfo != null)
            {
                //Values from Customer table
                lblAccount.Text = Users.GetAccountName(webuserid);
                txtCompany.Text = userInfo.Company;
                txtFName.Text = userInfo.FName;
                txtLName.Text = userInfo.LName;
                txtAddress.Text = userInfo.Address;
                txtAddress2.Text = userInfo.Address2;
                txtCity.Text = userInfo.City;
                ddState.SelectedValue = userInfo.State;
                txtZip.Text = userInfo.Zip.ToString();
                txtPhone.Text = Variables.PhoneNumberFormat(userInfo.Phone);
                txtPhone2.Text = Variables.PhoneNumberFormat(userInfo.Phone2);
                hiddenEntityId.Value = userInfo.id.ToString();
                txtEmail.Text = userInfo.Email;

            }
        }
    }