Filling a dropdownlistbox with two items part 2

G

Guest

I have altered to code to read as follows

public void BindSystemCodeList(

int i = 0
sysCodeList.DataSource = CreateSystemCodeListDataSource()
int count = CreateSystemCodeListDataSource().Count

for (i = 0; i <= count; i++

sysCodeList.Items.Text = "text here"
sysCodeList.Items.Value = "Value" + " - " + "Valdesc"

for (i = 0; i <= count; i++

string[] str = sysCodeList.Items.Value.ToString().Split('-')


Now when I run the program I get the following error

Server Error in '/FiveYearPlan' Application
-------------------------------------------------------------------------------

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: inde

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection
Parameter name: index
System.Collections.ArrayList.get_Item(Int32 index) +9
System.Web.UI.WebControls.ListItemCollection.get_Item(Int32 index) +1
FiveYearPlan.defaultForm.BindSystemCodeList() +7
FiveYearPlan.defaultForm.Page_Load(Object sender, EventArgs e) +16
System.Web.UI.Control.OnLoad(EventArgs e) +6
System.Web.UI.Control.LoadRecursive() +3
System.Web.UI.Page.ProcessRequestMain() +73



-------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

Can anyone help me with this error

Thanks

Dav
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

Two things. First, make one call to CreateSystemCodeListDataSource. No
need to run twice and waste those cycles.

Second, your loop is using i <= count. Change to i < count. The index
count does not exist, since the collection starts at 0.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave Bailey said:
I have altered to code to read as follows:

public void BindSystemCodeList()
{
int i = 0;
sysCodeList.DataSource = CreateSystemCodeListDataSource();
int count = CreateSystemCodeListDataSource().Count;

for (i = 0; i <= count; i++)
{
sysCodeList.Items.Text = "text here";
sysCodeList.Items.Value = "Value" + " - " + "Valdesc";
}
for (i = 0; i <= count; i++)
{
string[] str = sysCodeList.Items.Value.ToString().Split('-');
}

Now when I run the program I get the following error:

Server Error in '/FiveYearPlan' Application.
-------------------------------------------------------------------------- ------

Index was out of range. Must be non-negative and less than the size of the

collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of
range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error:

An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the exception
can be identified using the exception stack trace below.
Stack Trace:


[ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +91
System.Web.UI.WebControls.ListItemCollection.get_Item(Int32 index) +10
FiveYearPlan.defaultForm.BindSystemCodeList() +73
FiveYearPlan.defaultForm.Page_Load(Object sender, EventArgs e) +160
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731




-------------------------------------------------------------------------- ------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

Can anyone help me with this error?

Thanks,

Dave
 
N

Nico Vrouwe

Hi Dave,

The out of range error shouldn't be happening in this part of your code,
unless the value returned from CreateSystemCodeListDataSource() doesn't have
the same count as sysCodeList.Items.
Try running your application in debug mode (Menu Debug -> Start, or F5 if
you use VC6 IDE settings) and it'll show you exactly where the error occurs.

Hope this helps,

Nico Vrouwe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top