Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

IEnumerable bug

Posted on 2015-02-05
4
Medium Priority
?
114 Views
Last Modified: 2015-02-07
Hello,
I am having some problem with IEnumberable<string>
I get this error in the function Send.
Additional information: ERROR: 306 Incorrect msisdn parameter in request

string myText = "Some text";
string idString = "27";
SMSSender mySender = new SMSSender("mmmmm", "nnnnn");
IEnumerable<string> m_oEnum = new List<string>(){ idString };
mySender.Send(m_oEnum, myText);

 public void Send(IEnumerable<string> recipients, string text)
        {
            const int chunkSize = 255;
            while (recipients.Any())
            {
                var chunk = recipients.Take(chunkSize);
                recipients = recipients.Skip(chunkSize);
                var client = new HttpClient();
                var content = new FormUrlEncodedContent(new[] {
                    new KeyValuePair<string, string>("L", this.username),
                    new KeyValuePair<string, string>("P", this.password),
                    new KeyValuePair<string, string>("MSISDN", string.Join(",", chunk)),
                    new KeyValuePair<string, string>("T", text)
                });
                var response = client.PostAsync(this.uri, content).Result;
                response.EnsureSuccessStatusCode();
                var body = response.Content.ReadAsStringAsync().Result;
                if (body.StartsWith("ERROR")) throw new Exception(body);
            }
        }


If I write the number directly in like this it works.
IEnumerable<string> m_oEnum = new List<string>(){ "27"};

Do you know what I am doing wrong?
0
Comment
Question by:johnson1
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40592665
This isn't so much an answer to your problem as it is a suggestion, but you've got an endless loop. Calling Take does not actually remove elements from an enumerable--truth be told, you can't remove elements from an enumerable. So your call to Any will always return true, assuming there is at least one element in the enumerable. You'll want to change the condition of your loop such that you have a valid exit condition.
0
 

Author Comment

by:johnson1
ID: 40593605
If I do it like this it works:

SMSSender mySender = new SMSSender("mmmmm", "nnnnn");
IEnumerable<string> m_oEnum = new List<string>(){ "27"};

but it does not work if I do it like this

string idString = "27";
SMSSender mySender = new SMSSender("mmmmm", "nnnnn");
IEnumerable<string> m_oEnum = new List<string>(){ idString};

So the question is why can I not use idString instead of "27"
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 40594707
What VS/.NET version are you using?
The code below seems to work OK in VS 2013 (.NET 4.5.1)
        public static string GetDemo()
        {
            string data = "27";
            IEnumerable<string> m_oEnum = new List<string>() { data };
            var chunk = m_oEnum.Take(255);
            return string.Join(",", chunk);
        }

Open in new window

It should be easier for you if you use the constructor instead of the collection initializer:
IEnumerable<string> m_oEnum = new List<string>(new string[] { data });

Open in new window

0
 

Author Closing Comment

by:johnson1
ID: 40595336
Thank you. By using the constructor then it worked.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question