protected void btnMemberPrev_Click(object sender, EventArgs e)
        {
            SForce.SforceService sfdcbindingMembership = new SForce.SforceService();
            SForce.LoginResult   LoginResult           = sfdcbindingMembership.login(strUserName, strPasswd);
            sfdcbindingMembership.Url = LoginResult.serverUrl;
            sfdcbindingMembership.SessionHeaderValue = new SForce.SessionHeader();

            sfdcbindingMembership.SessionHeaderValue.sessionId = LoginResult.sessionId;
            queryResultMembership = sfdcbindingMembership.query(strQuerySelectFromMembership);


            int nMemberIndex = (int)Session["MemberIndex"];

            if (nMemberIndex > 0)
            {
                nMemberIndex--;
            }

            Session["MemberIndex"] = nMemberIndex;

            SForce.Membership__c membership = (SForce.Membership__c)queryResultMembership.records[nMemberIndex];
            txtContactId.Text        = membership.Id;
            txtMembershipName.Text   = membership.Name;
            txtMembershipStatus.Text = membership.c4g_Membership_Status__c;
        }
 protected void InitializedSfdcbinding()
 {
     Sfdcbinding                              = new SForce.SforceService();
     CurrentLoginResult                       = Sfdcbinding.login(strUserName, strPasswd);
     Sfdcbinding.Url                          = CurrentLoginResult.serverUrl;
     Sfdcbinding.SessionHeaderValue           = new SForce.SessionHeader();
     Sfdcbinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
 }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strId = (string)Session["CurrentId"];

            SForce.Contact contact_update = new SForce.Contact();

            contact_update.Id = strId;

            contact_update.FirstName = txtFirstName.Text;
            contact_update.LastName  = txtLastName.Text;

            contact_update.MailingStreet     = txtStreetAddress.Text;
            contact_update.MailingCity       = txtCity.Text;
            contact_update.MailingState      = txtState.Text;
            contact_update.MailingPostalCode = txtZip.Text;
            contact_update.MobilePhone       = txtMobilePhone.Text;
            contact_update.Phone             = txtPhone.Text;
            contact_update.Email             = txtEmail.Text;

            SForce.SforceService sfdcbinding = new SForce.SforceService();

            SForce.LoginResult login_result = null;

            login_result    = sfdcbinding.login(strUserName, strPasswd);
            sfdcbinding.Url = login_result.serverUrl;
            sfdcbinding.SessionHeaderValue           = new SForce.SessionHeader();
            sfdcbinding.SessionHeaderValue.sessionId = login_result.sessionId;

            SForce.SaveResult[] saveResults = sfdcbinding.update(new SForce.sObject[] { contact_update });

            if (saveResults[0].success)
            {
                lblErrorMessage.Text = "The record id: " + saveResults[0].id + " is updated successfully.";

                Sfdcbinding                    = new SForce.SforceService();
                CurrentLoginResult             = Sfdcbinding.login(strUserName, strPasswd);
                Sfdcbinding.Url                = CurrentLoginResult.serverUrl;
                Sfdcbinding.SessionHeaderValue = new SForce.SessionHeader();

                Sfdcbinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
                strQuerySelectFromContact = "select Id, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone from Contact order by LastName nulls last";
                queryResult = Sfdcbinding.query(strQuerySelectFromContact);

                nTotalRecord = queryResult.size - 1;
            }
            else
            {
                lblErrorMessage.Text = "Error: " + saveResults[0].errors[0].message;
            }

            btnCancel.Enabled = false;
            btnUpdate.Enabled = false;
        }
        protected void btnUpdateComment_Click(object sender, EventArgs e)
        {
            //string strId = (string)Session["CurrentId"];

            SForce.Task task_update = new SForce.Task();

            string Id = (string)Session["CurrentTaskId"];

            task_update.Id          = Id;
            task_update.Description = txtTaskComment.Text;

            //contact_update.Id = strId;

            //contact_update.FirstName = txtFirstName.Text;
            //contact_update.LastName = txtLastName.Text;

            //contact_update.MailingStreet = txtStreetAddress.Text;
            //contact_update.MailingCity = txtCity.Text;
            //contact_update.MailingState = txtState.Text;
            //contact_update.MailingPostalCode = txtZip.Text;
            //contact_update.MobilePhone = txtMobilePhone.Text;
            //contact_update.Phone = txtPhone.Text;
            //contact_update.Email = txtEmail.Text;



            SForce.SforceService sfdcbinding = new SForce.SforceService();

            SForce.LoginResult login_result = null;

            login_result    = sfdcbinding.login(strUserName, strPasswd);
            sfdcbinding.Url = login_result.serverUrl;
            sfdcbinding.SessionHeaderValue           = new SForce.SessionHeader();
            sfdcbinding.SessionHeaderValue.sessionId = login_result.sessionId;

            SForce.SaveResult[] saveResults = sfdcbinding.update(new SForce.sObject[] { task_update });

            if (saveResults[0].success)
            {
                lblTaskCommentUpdateResult.Text = "Success!";
            }
            else
            {
                lblTaskCommentUpdateResult.Text = saveResults[0].errors[0].message;
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strId = (string)Session["CurrentId"];

            String[] ids = new String[] { strId };

            SForce.SforceService sfdcbinding = new SForce.SforceService();

            SForce.LoginResult login_result = null;

            login_result    = sfdcbinding.login(strUserName, strPasswd);
            sfdcbinding.Url = login_result.serverUrl;
            sfdcbinding.SessionHeaderValue           = new SForce.SessionHeader();
            sfdcbinding.SessionHeaderValue.sessionId = login_result.sessionId;

            SForce.DeleteResult[] deleteResults = sfdcbinding.delete(ids);
            SForce.DeleteResult   deleteResult  = deleteResults[0];

            if (deleteResult.success)
            {
                lblErrorMessage.Text = "The record id: " + deleteResult.id + " is deleted successfully.";
                int nCurrentIndex = (int)Session["CurrentIndex"];

                nCurrentIndex--;
                //contact = (SForce.Contact)queryResult.records[nCurrentIndex];

                Sfdcbinding                    = new SForce.SforceService();
                CurrentLoginResult             = Sfdcbinding.login(strUserName, strPasswd);
                Sfdcbinding.Url                = CurrentLoginResult.serverUrl;
                Sfdcbinding.SessionHeaderValue = new SForce.SessionHeader();

                Sfdcbinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
                strQuerySelectFromContact = "select Id, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone from Contact order by LastName nulls last";
                queryResult = Sfdcbinding.query(strQuerySelectFromContact);

                nTotalRecord = queryResult.size - 1;

                contact = (SForce.Contact)queryResult.records[nCurrentIndex];

                txtFirstName.Text = contact.FirstName;
                txtLastName.Text  = contact.LastName;

                if (contact.MailingAddress != null)
                {
                    txtStreetAddress.Text = contact.MailingAddress.street;
                    txtCity.Text          = contact.MailingAddress.city;
                    txtState.Text         = contact.MailingAddress.state;
                    txtZip.Text           = contact.MailingAddress.postalCode;
                }
                else if (contact.MailingAddress == null)
                {
                    txtStreetAddress.Text = "";
                    txtCity.Text          = "";
                    txtState.Text         = "";
                    txtZip.Text           = "";
                }

                txtMobilePhone.Text     = contact.MobilePhone;
                txtPhone.Text           = contact.Phone;
                txtEmail.Text           = contact.Email;
                Session["CurrentIndex"] = nCurrentIndex;

                if (nCurrentIndex == 0)
                {
                    btnPrev.Enabled  = false;
                    btnFirst.Enabled = false;
                }

                strId = contact.Id;
                Session["CurrentId"] = strId;
            }
            else
            {
                lblErrorMessage.Text = "Error: " + deleteResult.errors[0].message;
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtLastName.Text == null)
            {
                lblErrorMessage.Text = "Error: You must enter your last name";
            }
            else
            {
                SForce.Contact sfdcContact = new SForce.Contact();
                sfdcContact.FirstName = txtFirstName.Text;
                sfdcContact.LastName  = txtLastName.Text;

                sfdcContact.MailingStreet     = txtStreetAddress.Text;
                sfdcContact.MailingCity       = txtCity.Text;
                sfdcContact.MailingState      = txtState.Text;
                sfdcContact.MailingPostalCode = txtZip.Text;
                sfdcContact.MobilePhone       = txtMobilePhone.Text;
                sfdcContact.Phone             = txtPhone.Text;
                sfdcContact.Email             = txtEmail.Text;

                SForce.SforceService sfdcbinding  = new SForce.SforceService();
                SForce.LoginResult   login_result = null;

                login_result    = sfdcbinding.login(strUserName, strPasswd);
                sfdcbinding.Url = login_result.serverUrl;
                sfdcbinding.SessionHeaderValue           = new SForce.SessionHeader();
                sfdcbinding.SessionHeaderValue.sessionId = login_result.sessionId;
                SForce.SaveResult[] saveResults = sfdcbinding.create(new SForce.sObject[] { sfdcContact });

                if (saveResults[0].success)
                {
                    lblErrorMessage.Text    = "You added a new record successfully.";
                    nCurrentIndex           = nTotalRecord;
                    Session["CurrentIndex"] = nCurrentIndex;
                    nTotalRecord++;

                    Sfdcbinding                    = new SForce.SforceService();
                    CurrentLoginResult             = Sfdcbinding.login(strUserName, strPasswd);
                    Sfdcbinding.Url                = CurrentLoginResult.serverUrl;
                    Sfdcbinding.SessionHeaderValue = new SForce.SessionHeader();

                    Sfdcbinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
                    strQuerySelectFromContact = "select Id, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone from Contact order by LastName nulls last";
                    queryResult = Sfdcbinding.query(strQuerySelectFromContact);

                    nTotalRecord = queryResult.size - 1;
                }
                else
                {
                    lblErrorMessage.Text = saveResults[0].errors[0].message;
                }

                btnFirst.Enabled = true;
                btnPrev.Enabled  = true;
                btnNext.Enabled  = true;
                btnLast.Enabled  = true;

                btnSave.Enabled   = false;
                btnCancel.Enabled = false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            strUserName = "******";
            strPasswd   = "speed5of5light5";

            if (!IsPostBack)
            {
                nCurrentIndex     = 0;
                nTotalRecord      = 0;
                nCurrentIndexTask = 0;
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;

                //Sfdcbinding = new SForce.SforceService();
                //CurrentLoginResult = Sfdcbinding.login(strUserName, strPasswd);
                //Sfdcbinding.Url = CurrentLoginResult.serverUrl;
                //Sfdcbinding.SessionHeaderValue = new SForce.SessionHeader();

                //Sfdcbinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
                //strQuery = "select Id, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone from Contact order by LastName nulls last";
                //queryResult = Sfdcbinding.query(strQuery);

                //nTotalRecord = queryResult.size - 1;

                btnCreateNewContact.Enabled = false;
                btnSave.Enabled             = false;
                btnEdit.Enabled             = false;
                btnUpdate.Enabled           = false;
                btnCancel.Enabled           = false;
                btnDelete.Enabled           = false;

                txtFirstName.ReadOnly     = true;
                txtLastName.ReadOnly      = true;
                txtStreetAddress.ReadOnly = true;
                txtCity.ReadOnly          = true;
                txtState.ReadOnly         = true;
                txtZip.ReadOnly           = true;
                txtPhone.ReadOnly         = true;
                txtMobilePhone.ReadOnly   = true;
                txtEmail.ReadOnly         = true;

                //strQuerySelectFromContact = "select Id, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone from Contact order by LastName nulls last";
                //strQuerySelectFromMembership = "select Id, Membership.c4g_Membership_Status__c, c4g_Number_of_Gold_Medi_Members__c from Membership nulls last";
                //strQuerySelectFromContactIncludeMembership = "select Contact.Id, Contact.FirstName, Contact.LastName " +
                //                                                "(select Membership.c4g_Membership_Status__c, c4g_Number_of_Gold_Medi_Members__c, " +
                //                                                "c4g_Number_of_Gold_Members__c, c4g_Number_of_Gold_Plus_Members__c, " +
                //                                                "Number_of_Members_Units__c, Paying_Member__c, " +
                //                                                "Registration_Date__c, c4g_Start_Date__c " +
                //                                                "from Membership) from Contact " +
                //                                                "where Contact.Id in (select Membership.ContactId from Membership) nulls last";
            }

            //strQuerySelectFromContact = "select Id, c4g_Membership__c, Individual_ID__c, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone from Contact order by LastName nulls last";


            strQueryMembershipIdInContact = "select Contact.c4g_Membership__c from Contact order by LastName nulls last";



            strQuerySelectFromMembership = "select Id, Name, c4g_Membership_Status__c from Membership__c";



            /// This is the current working inner join version
            strQueryContactInnerJoinMembership = "select Id, Individual_ID__c, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone, " +
                                                 "(select Name, Id from Memberships__r) from Contact " +
                                                 "where Contact.c4g_Membership__c in (select Membership__c.Id from Membership__c) " +
                                                 "order by Contact.LastName, Contact.FirstName nulls last";


            // SELECT Name, (SELECT Name FROM Job_Applications__r) FROM Position__c

            // This is the Left Outer Join version.
            strQueryContactLeftOuterJoinMembership = "select Id, Individual_ID__c, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone, " +
                                                     "c4g_Membership__r.Name, c4g_Membership__r.Id from Contact " +
                                                     "order by Contact.LastName, Contact.FirstName nulls last";

            strQueryTask = "select Id, Description from Task";

            Sfdcbinding                    = new SForce.SforceService();
            CurrentLoginResult             = Sfdcbinding.login(strUserName, strPasswd);
            Sfdcbinding.Url                = CurrentLoginResult.serverUrl;
            Sfdcbinding.SessionHeaderValue = new SForce.SessionHeader();

            Sfdcbinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
            //queryResult = Sfdcbinding.query(strQuerySelectFromContact);

            // This is Contact Inner Join Membership
            //queryResult = Sfdcbinding.query(strQueryContactInnerJoinMembership);

            // This is Contact Outer Join Membership

            queryResultTask = Sfdcbinding.query(strQueryTask);
            queryResult     = Sfdcbinding.query(strQueryContactLeftOuterJoinMembership);

            nTotalRecord = queryResult.size - 1;

            ////////////////////////////////////////////////////////////////////////////////
            // Testing for select from membership
            //SForce.SforceService sfdcbindingMembership = new SForce.SforceService();
            //SForce.LoginResult LoginResult = sfdcbindingMembership.login(strUserName, strPasswd);
            //sfdcbindingMembership.Url = LoginResult.serverUrl;
            //sfdcbindingMembership.SessionHeaderValue = new SForce.SessionHeader();

            //sfdcbindingMembership.SessionHeaderValue.sessionId = LoginResult.sessionId;
            //SForce.QueryResult QueryResultMembership = sfdcbindingMembership.query(strQuerySelectFromMembership);
        }
        protected void btnLoad_Click(object sender, EventArgs e)
        {
            //Sfdcbinding = new SForce.SforceService();
            //CurrentLoginResult = Sfdcbinding.login(strUserName, strPasswd);
            //Sfdcbinding.Url = CurrentLoginResult.serverUrl;
            //Sfdcbinding.SessionHeaderValue = new SForce.SessionHeader();

            //Sfdcbinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;
            //strQuerySelectFromContact = "select Id, FirstName, LastName, MailingAddress, Email, MobilePhone, Phone from Contact order by LastName nulls last";
            //queryResult = Sfdcbinding.query(strQuerySelectFromContact);

            //nTotalRecord = queryResult.size - 1;

            if (queryResult.size > 0)
            {
                contact = (SForce.Contact)queryResult.records[nCurrentIndex];

                txtIndividualId.Text = contact.Individual_ID__c;
                txtId.Text           = contact.Id;
                //SForce.Membership__c member = (SForce.Membership__c) contact.Memberships__r.records[0];

                if (contact.c4g_Membership__r != null)
                {
                    txtMembershipId.Text            = contact.c4g_Membership__r.Id;
                    txtMembershipNameInContact.Text = contact.c4g_Membership__r.Name;
                }
                else
                {
                    txtMembershipId.Text            = "";
                    txtMembershipNameInContact.Text = "";
                }


                //if (contact.c4g_Membership__r != null)
                //{
                //    txtMembershipId.Text = contact.c4g_Membership__r.Id;
                //    txtMembershipInContact.Text = contact.c4g_Membership__r.Name;
                //}
                //else if (contact.c4g_Membership__r == null)
                //{
                //    txtMembershipId.Text = "";
                //    txtMembershipInContact.Text = "";
                //}


                //txtMembershipId.Text = member.Id;
                //txtMembershipNameInContact.Text = member.Name; ;
                txtFirstName.Text = contact.FirstName;
                txtLastName.Text  = contact.LastName;

                if (contact.MailingAddress != null)
                {
                    txtStreetAddress.Text = contact.MailingAddress.street;
                    txtCity.Text          = contact.MailingAddress.city;
                    txtState.Text         = contact.MailingAddress.state;
                    txtZip.Text           = contact.MailingAddress.postalCode;
                }
                else if (contact.MailingAddress == null)
                {
                    txtStreetAddress.Text = "";
                    txtCity.Text          = "";
                    txtState.Text         = "";
                    txtZip.Text           = "";
                }
                txtMobilePhone.Text = contact.MobilePhone;
                txtPhone.Text       = contact.Phone;
                txtEmail.Text       = contact.Email;
            }

            btnFirst.Enabled            = false;
            btnPrev.Enabled             = false;
            btnCreateNewContact.Enabled = true;
            btnEdit.Enabled             = true;
            btnDelete.Enabled           = true;
            lblErrorMessage.Text        = "";

            ////////////////////////////////////////////////////////////////////////////////
            // Testing for select from membership
            SForce.SforceService sfdcbindingMembership = new SForce.SforceService();
            SForce.LoginResult   LoginResult           = sfdcbindingMembership.login(strUserName, strPasswd);
            sfdcbindingMembership.Url = LoginResult.serverUrl;
            sfdcbindingMembership.SessionHeaderValue = new SForce.SessionHeader();

            sfdcbindingMembership.SessionHeaderValue.sessionId = LoginResult.sessionId;
            queryResultMembership = sfdcbindingMembership.query(strQuerySelectFromMembership);


            //strQueryMembershipIdInContact

            /////////////////////////////////////
            // Testing for Membership field in Contact
            SForce.QueryResult queryMembershipInContact = sfdcbindingMembership.query(strQueryMembershipIdInContact);
            //SForce.QueryResult queryContactInnerJoinMembership = sfdcbindingMembership.query(strQueryContactInnerJoinMembership);

            if (queryMembershipInContact.size > 0)
            {
                SForce.Contact contactMembership = (SForce.Contact)queryMembershipInContact.records[0];

                txtMembershipInContact.Text = contactMembership.c4g_Membership__c;
            }

            if (queryResultMembership.size > 0)
            {
                SForce.Membership__c membership = (SForce.Membership__c)queryResultMembership.records[0];
                //SForce.Contact contact = (SForce.Contact)queryResultMembership.records[0];

                txtContactId.Text        = membership.Id;
                txtMembershipName.Text   = membership.Name;
                txtMembershipStatus.Text = membership.c4g_Membership_Status__c;

                //txtContactId.Text = contact.Id;
                //txtMembershipName.Text = contact.Name; ;  //membership.Name;
                //txtMembershipStatus.Text = contact.c4g_Membership_Status__c;   //membership.c4g_Membership_Status__c;
                int nMemberIndex = 0;
                Session["MemberIndex"] = nMemberIndex;
            }

            if (queryResultTask.size > 0)
            {
                SForce.Task task = (SForce.Task)queryResultTask.records[nCurrentIndexTask];

                txtTaskName.Text    = task.Id;
                txtTaskComment.Text = task.Description;

                Session["TaskIndex"] = ++nCurrentIndexTask;

                Session["CurrentTaskId"] = task.Id;
            }
        }