Solved

IEnumerable bug

Posted on 2015-02-05
4
85 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 35

Accepted Solution

by:
Miguel Oz earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
Sum Column in GridView 3 44
Parse the OuterHTML Data in the txtfile using c# 4 48
How useful is the free version of Selenium? 3 13
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now