function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TsukasaTsukasa 

Underlying connection error and URL not reset error

Here are the 2 errors I currently have with my application and looking for some pointers.

My app searches the tickets every 60seconds

I have used the windows 7 sdk and converted the wsdl file to .cs

 

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

UNKNOWN_EXCEPTION: Destination URL not reset. The URL returned from login must be set in the SforceService

 

Here is the current code i'm working with

SalesForce.cs

public class SalesForce
    {
        private SforceService binding = new SforceService();

        public bool logout()
        {
            try
            {
                binding.logout();
                return true;
            }
            catch (Exception ex)
            {
                EZDebug.SendError(ex.Message, "SF Logout");
                return false;
            }
        }

        public bool login(string username, string password, string token)
        {
            binding.Timeout = 60000;

            LoginResult lr;
            try
            {
                lr = binding.login(username, password + token);
            }
            catch (SoapException ex)
            {
                EZDebug.SendError(ex.Message, "Login Error");
                return false;
            }

            if (lr.passwordExpired)
            {
                MessageBox.Show("Password Expired");
                return false;
            }

            binding.Url = lr.serverUrl;
            binding.SessionHeaderValue = new SessionHeader();
            binding.SessionHeaderValue.sessionId = lr.sessionId;
            return true;
        }

        public DataTable sfQ(string department)
        {
            DataTable ticketList = new DataTable("tickets");
            DataColumn sfstatus = new DataColumn("status", typeof(System.String));
            DataColumn sfaccountid = new DataColumn("accountid", typeof(System.String));
            DataColumn sfclientid = new DataColumn("clientid", typeof(System.String));
            DataColumn sfticketnumber = new DataColumn("ticketnumber", typeof(System.String));
            DataColumn sfsubject = new DataColumn("subject", typeof(System.String));
            DataColumn sfesc = new DataColumn("esc", typeof(System.String));
            ticketList.Columns.Add(sfstatus);
            ticketList.Columns.Add(sfaccountid);
            ticketList.Columns.Add(sfclientid);
            ticketList.Columns.Add(sfticketnumber);
            ticketList.Columns.Add(sfsubject);
            ticketList.Columns.Add(sfesc);
            ticketList.PrimaryKey = new DataColumn[] {sfticketnumber};

            string statment = "select IsEscalated, Customer_Number__c, AccountId, Subject, CaseNumber, Product_Category__c, Status from Case where Product_Category__c = '" + department + "' and (Status = 'New'or Status = 'Client Called Back' or Status = 'Client Called Back - 2nd Time!' or Status = 'Client Called Back - Escalated')";
            QueryResult qr = null;
            QueryResult qr2 = null;
            binding.QueryOptionsValue = new QueryOptions();
            binding.QueryOptionsValue.batchSize = 250;
            binding.QueryOptionsValue.batchSizeSpecified = true;
            try
            {
                qr = binding.query(statment);
                bool done = false;
                if (qr.size > 0)
                {
                    while (!done)
                    {
                        for (int i = 0; i < qr.records.Length; i++)
                        {
                            Case sfcase = (Case)qr.records[i];
                            string gbsclientid = sfcase.Customer_Number__c;
                            string stat = sfcase.Status;
                            string tn = sfcase.CaseNumber;
                            string subj = sfcase.Subject;
                            string clientid = sfcase.AccountId;
                            string esc = "";
                            esc = sfcase.IsEscalated.ToString();
                            qr2 = binding.query("select id, Name from Account where id = '" + clientid + "'");
                            Account account = (Account)qr2.records[0];
                            string accountid = account.Name;

                            ticketList.Rows.Add(stat, accountid, gbsclientid, tn, subj, esc);

                        }

                        if (qr.done)
                        {
                            done = true;
                        }
                        else
                        {
                            qr = binding.queryMore(qr.queryLocator);
                        }
                    }
                    return ticketList;
                }
                else
                {
                    ticketList.Clear();
                    return ticketList;
                }
            }
            catch (Exception ex)
            {
                EZDebug.SendError(ex.Message, "SFQ Error");
                if (login(Global.sfUsername, Global.sfPassword, Global.sfToken))
                {
                    sfQ(Global.Department);
                }
                ticketList.Clear();
                return ticketList;
            }
        }
    }

 

Main.cs

private void backgroundWorkerCallTracker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                SalesForce sf = new SalesForce();
                RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\EZC");
                string department = "";
                if (rk.GetValue("SFD") != null)
                {
                    Global.Department = department = Crypto.Decrypt(rk.GetValue("SFD").ToString(), true);
                }
                else
                {
                    EZDebug.SendError("Department not set", "Call Tracking");
                    MessageBox.Show("Call Tracker will not be enabled until you set your current Deparment in the Sales Force Settings Menu" + Environment.NewLine + "Once you have set you options, please close and reopen EZC", "Call Tracker Disabled", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    backgroundWorkerCallTracker.Dispose();
                }
                if (sf.login(Global.sfUsername, Global.sfPassword, Global.sfToken))
                {
                    for (; ;)
                    {
                        Global.TicketHolder = sf.sfQ(department);
                        if (Global.TicketHolder != null)
                        {
                            if (!this.InvokeRequired)
                                return;
                                this.Invoke((Delegate)new MethodInvoker(CallTracking));
                        }

                        int timeout = 0;
                        while (true)
                        {
                            timeout = (60 - DateTime.Now.Second) * 1000 - DateTime.Now.Millisecond;
                            Thread.Sleep(timeout);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("login failed");
                }
            }
            catch (Exception ex)
            {
                EZDebug.SendError(ex.Message, "TicketTracker");
                backgroundWorkerCallTracker.RunWorkerAsync();
            }
        }

 

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Global.VPNType == "Cisco")
            {
                VPNConnect.DCisco();
            }
            else if (Global.VPNType == "Sonicwall")
            {
                VPNConnect.DSonicwall();
            }
            else
            {
                new SalesForce().logout();
                Environment.Exit(1);
            }
        }