Data Binding Problem in DropDownList Control SelectedValue to Table text field

  • Thread starter Thread starter Tom Jorgenson
  • Start date Start date
T

Tom Jorgenson

I have a DropDownList whose items are populated simply via the Items
property of the control. The field represents the gender of a client and the
items are simply 'Male', 'Female', and 'Choose One'.

I want to bind the selected item to a table record where the field is a text
field, that is, I want the stored field to be (for example) 'Male' rather
than a foreign key or index value. The field has a default value of 'Choose
One'.

This would seem to be straight-forward enough. I would think that I should
simply bind the SelectedValue property to the specified table field. But it
doesn't work. Specifically...

When the DataBind() method is called an ArgumentOutOfRangeException error is
generated. I thought this might be due to the width of the data field (15
characters), which causes the stored value to be padded with spaces (i.e.
'Choose One ' rather than 'Choose One') so I simply tried to change the
Items property to hold items padded out similarly (figuring I'd try to find
a way to trim the field via the Format later). No change.

What am I doing wrong?

Thanks!
 
Tom,

The error you are seeing probably isn't due to the width of the field.

It could be due to the type of database you are using. I've seen some weird
things happen with datatypes when I pull from old legacy databases like our
AS400.

But the error you are receiving sounds more like no records are being
returned. Have you checked that your dataset / datatable / datareader
(whichever you're using) is actually returning records?

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
Try this
SelectedIndex.Text not SelectedIndex.Value
because you don't want to use primary key.
Tom
-----Original Message-----
I have a DropDownList whose items are populated simply via the Items
property of the control. The field represents the gender of a client and the
items are simply 'Male', 'Female', and 'Choose One'.

I want to bind the selected item to a table record where the field is a text
field, that is, I want the stored field to be (for example) 'Male' rather
than a foreign key or index value. The field has a default value of 'Choose
One'.

This would seem to be straight-forward enough. I would think that I should
simply bind the SelectedValue property to the specified table field. But it
doesn't work. Specifically...

When the DataBind() method is called an
ArgumentOutOfRangeException error is
 
Back
Top