Data Binding Problem in DropDownList Control SelectedValue to Table text field

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!
 
S

S. Justin Gengo

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
 
G

Guest

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
 
Joined
Mar 9, 2007
Messages
1
Reaction score
0
I think I've got the same problem.

Note that I'm using 2003 and .Net 1.1 - I'll try in 2.0 when I get a chance.
The control is a ComboBox with DropDownStyle DropDownList.

Also the problem is about binding the selected value / edit text rather than binding the list of items in the drop down (i.e. simple data binding):

comboBoxGender.DataBindings.Add("Text",contact,"Gender");

Using the "Text" property rather than "SelectedValue" doesn't help, unless you change the style DropDownStyle to DrowDown in which case it works fine.

Seems like this should be a really simple thing to do!
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top