Data Binding Problem in DropDownList Control SelectedValue to Table text field

Discussion in 'ASP .Net' started by Tom Jorgenson, Dec 10, 2003.

  1. 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 Jorgenson, Dec 10, 2003
    #1
    1. Advertising

  2. 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


    "Tom Jorgenson" <> wrote in message
    news:%...
    > 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. Justin Gengo, Dec 10, 2003
    #2
    1. Advertising

  3. Tom Jorgenson

    Guest 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
    >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!
    >
    >
    >.
    >
     
    Guest, Dec 10, 2003
    #3
  4. Tom Jorgenson

    Joined:
    Mar 9, 2007
    Messages:
    1
    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!
     
    , Mar 9, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gu4y
    Replies:
    4
    Views:
    6,862
    mngates
    Jan 13, 2010
  2. Thomas Kern
    Replies:
    1
    Views:
    433
    =?Utf-8?B?YWxhc3Bpbg==?=
    Nov 9, 2004
  3. TSA
    Replies:
    1
    Views:
    763
  4. Ben
    Replies:
    0
    Views:
    3,354
  5. Chris
    Replies:
    11
    Views:
    2,525
    Chris
    May 18, 2008
Loading...

Share This Page