Drop Down List Control Generates Index Out of Range Error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to populate a dropdownlistbox on a web form with two items from a database separated by a -(dash)

The code is as follows

public DataView CreateSystemCodeListDataSource(

string systemConnect = "Provider=\"MSDAORA.1\";" + GetConnectionString() + ""
string systemSelect = "Select Value, Valdesc from Valuelist where Listname = 'PLAN5YR' or"
" Listname = 'TYPE5YR' order by Value"

oda3 = new OleDbDataAdapter(systemSelect, systemConnect)
DataSet ds3 = new DataSet()
oda3.Fill(ds3, "syscode")
DataView syscode = ds3.Tables["syscode"].DefaultView
return syscode


public void BindSystemCodeList(

int i = 0
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('-')



When i run the program I get the following error

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

What do I need to change to make this work. I know the error happens here because if I comment this code out, the program runs as it should

Thanks

Dav



Print | Copy URL of this post



Expand All Collapse All


Manage Your Profile |Legal |Contact Us |MSDN Flash Newslette
 
Greets,

The problem is that you are trying to index items in the listbox which
do not already exist. If you are trying to add new values, use the Add()
method for the DropDownList.

Regards,

Joe

Dave Bailey said:
I am trying to populate a dropdownlistbox on a web form with two items
from a database separated by a -(dash).
The code is as follows:

public DataView CreateSystemCodeListDataSource()
{
string systemConnect = "Provider=\"MSDAORA.1\";" + GetConnectionString() + "";
string systemSelect = "Select Value, Valdesc from Valuelist where Listname = 'PLAN5YR' or" +
" Listname = 'TYPE5YR' order by Value";

oda3 = new OleDbDataAdapter(systemSelect, systemConnect);
DataSet ds3 = new DataSet();
oda3.Fill(ds3, "syscode");
DataView syscode = ds3.Tables["syscode"].DefaultView;
return syscode;
}

public void BindSystemCodeList()
{
int i = 0;
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('-');
}
}

When i run the program I get the following error:

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

collection. Parameter name: index
What do I need to change to make this work. I know the error happens here
because if I comment this code out, the program runs as it should.
 
Back
Top