Error filling DropdownList

T

tshad

Here is the DropDown:
<asp:DropDownList ID="ScreenTest" runat="server" />

Here is the code:
ScreenTest.DataSource=objCmd.ExecuteReader
ScreenTest.DataValueField="ScreenTemplateMasterID"
ScreenTest.DataTextField= "ScreenName"
ScreenTest.databind()
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

Here is the error
******************************************************************************************************************
Specified argument was out of the range of valid values. Parameter name:
value
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: Specified argument
was out of the range of valid values. Parameter name: value

Source Error:


Line 68: ScreenTest.DataValueField="ScreenTemplateMasterID"
Line 69: ScreenTest.DataTextField= "ScreenName"
Line 70: ScreenTest.databind()
<------- The error
Line 71: ScreenTest.Items.Insert(0, new ListItem("Select Screen
Test",""))
Line 72:
********************************************************************************************************************

Here is the trace of the DataReader Just before (Normally this is not there
as it would empty the reader)

ScreenName = Adminstrative Assistant
ScreenTemplateMasterID = 2
ScreenName = Network Analyst
ScreenTemplateMasterID = 1
ScreenName = Sales Clerk
ScreenTemplateMasterID = 4
ScreenName = Sales Manager
ScreenTemplateMasterID = 3

I assume the error is on the ScreenTemplateMasterID, but I don't see any
illegal values there.

What could have caused this?

Thanks,

Tom
 
G

Greg Burns

tshad said:
Here is the DropDown:
<asp:DropDownList ID="ScreenTest" runat="server" />

Here is the code:
ScreenTest.DataSource=objCmd.ExecuteReader
ScreenTest.DataValueField="ScreenTemplateMasterID"
ScreenTest.DataTextField= "ScreenName"
ScreenTest.databind()
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

Maybe you dropdown already has those values? Are you binding twice? Not
sure you would get that error or not.

Try adding this prior to your databind:
ScreenItems.Items.Clear()

Your code says this:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

But stack trace has this:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",""))

Regardless, I would turn on Option Strict and you see that this:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

Needs to be:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test","0"))

Or maybe it really is just:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",""))

and that was a typo. :^)

Greg
 
T

tshad

Greg Burns said:
Maybe you dropdown already has those values? Are you binding twice? Not
sure you would get that error or not.
No.

If that were the case I would have gotten the traces twice.
Try adding this prior to your databind:
ScreenItems.Items.Clear()

I tried that.

Didn't change anything.
Your code says this:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

But stack trace has this:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",""))

Regardless, I would turn on Option Strict and you see that this:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",0))

Needs to be:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test","0"))

Or maybe it really is just:
ScreenTest.Items.Insert(0, new ListItem("Select Screen Test",""))

and that was a typo. :^)

I had copied the error already and did a couple of other tests to see if I
could narrow it down. That was why that line was different. Actually, the
Double-Quotes is how I originally had it.

I also thought that maybe I had accidently created another object with that
name, but that wasn't the case either.

I took the SP and tried run it from Query Analyser and got the same results
as my trace.

ScreenTemplateMasterID ScreenName
---------------------- --------------------------------------------------
2 Adminstrative Assistant
1 Network Analyst
4 Sales Clerk
3 Sales Manager

(4 row(s) affected)

This doesn't make any sense.

It was working fine before.

Thanks,

Tom
 
G

Guest

Hi tshad,

When you DataBind the dropdown, it trys to reset itself to its previously
selected value.
Call ClearSelection() on it before you bind it.

Tim
 
T

tshad

timkling said:
Hi tshad,

When you DataBind the dropdown, it trys to reset itself to its previously
selected value.
Call ClearSelection() on it before you bind it.

Is that different than ScreenTest.Items.Clear()?

Thanks,
 
G

Guest

Yeah, it is different.

Whether you clear the Items collection, or you just call DataBind() (which
clears the collection for you first anyway), the dropdown "remembers" what
the selected value is. When you rebind it, it tries to select an item with
the same value, if there is none, it throws an exception. Calling
ClearSelection(), explicitly clears any selected value, so that it will not
try to reset the selected item while DataBinding.

Tim
 

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

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top