12 Replies - 2453 Views - Last Post: 04 October 2012 - 12:14 PM Rate Topic: -----

#1 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 354
  • Joined: 10-November 09

asp.net c# Index outside bounds of array....

Posted 03 October 2012 - 02:11 PM

Hey, all...

I just inherited this project from another developer and I need to get this running for a demo to a client.
First, let me explain what the situation is.

Essentially, we are dynamically creating a check box list from a data source
  • We are storing Names of PDF Files and Specific Values in one field in a table
  • They are delimited like so: Name.PDF(comma delimiter),Value(pipe delimiter)|....next pdf,next value...|etc...
  • We are splitting the string within loops : See code block below
  • You will see "f.Option_Values" or "f.Field_Number" FYI: f = a returned value of a LINQ expression


The following code block is part of a switch statement that determines what control(s) to generate - this just happens to be the one that doesn't work...

                        pnlcontent.Controls.Add(lbl);
                        var ck = new CheckBoxList();
                        ck.ID = f.Field_Number;
                        ck.Style.Add("float", "left");
                        foreach (var s in f.Option_Values.Split('|'))
                        {
                            string[] st = s.Split('~');
                            var it = new ListItem(st[0], st[1]);
                            ck.Items.Add(it);

                            foreach (var v in val.Split(','))
                            {
                                if (v == st[1])
                                {
                                    it.Selected = true;
                                }
                            }
                        }

                        tt.TargetControlID = ck.ID;
                        pnlcontent.Controls.Add(ck);
                        pnlcontent.Controls.Add(help);
                        pnlcontent.Controls.Add(ltl);



The error I get is this: Index was outside the bounds of the array.
and it errors out on this line: (see attached image)
                       string[] st = s.Split('~');



Any thoughts on this would be most appreciated.

PS
Yes, I use a lot of "regions" - just to keep the code neat and tidy... and I know where to find stuff. :P

Attached image(s)

  • Attached Image

This post has been edited by mb2000inc: 03 October 2012 - 02:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: asp.net c# Index outside bounds of array....

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: asp.net c# Index outside bounds of array....

Posted 03 October 2012 - 02:20 PM

Where is the tilde in the database records? You showed a pipe and a comma, but no tilde. If there isn't a tilde, then the result of string.split() won't have a position 1.
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: asp.net c# Index outside bounds of array....

Posted 03 October 2012 - 04:16 PM

Plus it won't even have an index 0, since it'll be and empty array.
Was This Post Helpful? 1
  • +
  • -

#4 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 05:54 AM

I thought that it would put the entire string into the zero position?

EDIT: If the delimiter doesn't exist in the string, the whole string will be placed into array index 0. And it will still be an array.

This post has been edited by h4nnib4l: 04 October 2012 - 12:17 PM

Was This Post Helpful? 1
  • +
  • -

#5 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 354
  • Joined: 10-November 09

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 06:28 AM

I'm not sure what the original creator of this was thinking when they added the tilde. According to the documentation, it was supposed to work "as-is" - but....... it doesn't.
If I remove the tilde - there's no item variable to split:
var it = new ListItem(st[0], st[1]);


So.... how would I get around that?
Thoughts?

This post has been edited by mb2000inc: 04 October 2012 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

#6 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 354
  • Joined: 10-November 09

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 07:40 AM

ok, if I remove the "~" I get both values "name.pdf,value" that show up - and I want it to take the value.... If I replace the "~" with a "," then I get only the first part "name.pdf".

How do I get the second value?
Was This Post Helpful? 0
  • +
  • -

#7 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 08:25 AM

If you're splitting on the comma, the value part should be showing up in st[1]. What's stored at that index?
Was This Post Helpful? 1
  • +
  • -

#8 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 354
  • Joined: 10-November 09

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 08:44 AM

Hi... Taking this:

Quote

Name.PDF(comma delimiter),Value(pipe delimiter)|


The Value at st[1] would be the item after the comma delimiter "Value".

Essentially, I have two PDF files that a user can choose from.
In the table field - I have "name.pdf" and "value". The Value is the actual name of the PDF without and special characters (I.E. "_", "-", etc... and no file extension)

Essentially, the users are choosing a "logo" (in PDF form) to add to their project one is something like: "Special_Logo.PDF" and the other is like "Extreme_Logo_TM.PDF".

The values for selection are "Special Logo" and "Extreme Logo TM". I'm trying to retrieve the "values" rather than the PDF names.
Was This Post Helpful? 0
  • +
  • -

#9 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 09:16 AM

The IndexOutOfRangeException should be fixed by switching the tilde to a comma, right?

If you have a string s = "something,somethingelse", and you perform var st = s.Split(','), then the value of st[0] will be "something" and st[1] will be "somethingelse". Aren't you just looking for what is inside st[1]??

I'm really confused as to exactly what you want. From what I can tell, you have a string that has all of your values. You run a foreach loop, splitting on the pipe ('|') to get the individual records. You split each string on the comma, and use the values to create a new ListItem, and then add it to your CheckBoxList. You run another foreach, splitting val on a comma, although I don't know what val is because we can't see it in the code. Beyond this, we can't tell anything because that's the only code we've seen. But it looks like you're expecting val to contain something that matches the format of s from the outer foreach loop, so you're splitting val on the comma (the same way you should be splitting s now), and comparing the "value" from each.

This post has been edited by h4nnib4l: 04 October 2012 - 09:17 AM

Was This Post Helpful? 1
  • +
  • -

#10 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 354
  • Joined: 10-November 09

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 11:01 AM

I know. I'm terrible with explanations (I fully understand and acknowledge your frustration with me)... Here, let me see if I can put it another way.

This is the problem code:
                                foreach (var s in f.Option_Values.Split('|'))
                                {
                                    string[] st = s.Split('~');
                                    var it = new ListItem(st[0], st[1]);
                                    ck.Items.Add(it);

                                    foreach (var v in val.Split(','))
                                    {
                                        if (v == st[1])
                                        {
                                            it.Selected = true;
                                        }
                                    }
                                }



You asked about "val".... it comes comes from this: (CustomFields is a class that was created in the Data Access Layer)
                CustomFields cf = (from CustomFields c in PODItem.Fields where c.FieldName == f.Field_Number select c).SingleOrDefault();
                var val = "";
                if (cf.Value != f.Friendly_Name)
                {
                    val = cf.Value;
                }



The string value is coming from the Table Field (f.Option_Values)
"f" comes from this (which is wrapped around the entire code block):
(pay no attention to "WizardStep" - it comes from a completely different function)
(and Print_Custom_Field is another class in my Data Access Layer)
            pnlcontent.Controls.Clear();
            foreach (Print_Custom_Field f in (from Print_Custom_Field p in _db.Print_Custom_Fields where p.Item_Number == ItemNumber && p.Print_Custom_Group_Number == WizardStep select p))
            {
                 //.......... more code
            }



Does that make a little more sense (as to where the other data is coming from)?

Now...
My "f.OptionValues" = "Special_Logo.PDF,Special Logo|Extreme_Logo_TM.PDF,Extreme Logo TM"
I want the values after each "comma" (and before any "pipes" if more than one).

When I remove the "~" from this line:
                        foreach (var s in f.Option_Values.Split('|'))
                        {
                            //this line here
                            string[] st = s.Split('~');
                            //....rest of code
                        }



My results look like the attached image. (NoSeparator.jpg)

If I replace the "~" with a "," - I get the second Attached image. (CommaSeparated.jpg)

Ok, so... Instead of the PDF Name (shown in Comma Separated image), I want the Name Values (Special Logo and Extreme Logo TM) to show up as the choices for the Check Box List...

Does that help?
Like I said - I'm terrible with explanations... and I apologize for the confusion(s). Any other thoughts are truly appreciated.

Side Note: If it were up to me, I'd re-write the application and go a whole different route with it - but there's no time to do so - the client needs it and I have to work with what I have in the time allotted.

Apologies again. :-)

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#11 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 354
  • Joined: 10-November 09

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 11:47 AM

WAIT!!!! HOLD UP!!!!

I got it! :D

I'm so dense... I finally figured out what you were suggesting!

I did the split on ',' this.... then removed "st[0],"... like so:

                        foreach (var s in f.Option_Values.Split('|'))
                        {
                            string[] st = s.Split(',');
                            var it = new ListItem(st[1]);
                            ck.Items.Add(it);

                            foreach (var v in val.Split(','))
                            {
                                if (v == st[1])
                                {
                                    it.Selected = true;
                                }
                            }
                        }



And it worked! Thank you VERY much for your assistance! You just made my day! :D
Was This Post Helpful? 1
  • +
  • -

#12 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 11:52 AM

EDIT: You posted while I was typing my reply. Good job.

This post has been edited by h4nnib4l: 04 October 2012 - 11:53 AM

Was This Post Helpful? 1
  • +
  • -

#13 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 354
  • Joined: 10-November 09

Re: asp.net c# Index outside bounds of array....

Posted 04 October 2012 - 12:14 PM

Thank you again! You rock! :punk:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1