Solution to: dropdownlist has a SelectedValue which is invalidbecause it does not exist in the list

K

ken_wilsonii

I ran into this problem it ended up being a combination of this that
needed to be done.
I believe this will work for a bound list to.

The whole problem stems from the two-way binding which has caused me
several other headaches.

My example is derived from binding a DETAILSVIEW to a SQLDATASOURCE.
The field is then converted to a template field for which I modify the
EditTemplate for that
field to dropdownlist and then bind it to another sqldatasource for
the dropdown list values.

The error occurs because I am trying to edit the data and the
datafield is empty.
The NULL value causes the error because the value is not in the
dropdown list.

Solution

1) modify the AppendDataBoundItems property of the dropdownlist and
set it to true
and make sure it's bound to field you need to update.

2) add an and Item to the Collection and use something similar to "--
Select Username --"
depends on what the user will be selecting. Make sure the select
is set to true.

3) Modify the Select query for the sqldatasource that the detailsview
is bound to
to account for the null value have it match what was added to
collection in step 2

ISNULL(Username,'-- Select Username --') as Username

4) Step 3 takes care of the error but now you must account for the Two-
Way Binding
and insure that '-- Select Username --' is not the value saved
back to the database if nothing
else is slected from the list.

Modify the Update query back to the data for the same datasource
so a null value
is applied just in case '-- Select Username --' is still selected.

Username = NULLIF(@Username,'-- Select Username --')

Best I could come up with.
 
J

Just Me

OK, here's another view on this.... ( Untested )

Instead of having "--Select Username" as a Value AND as Text. Make the
value "" for the Value that way an empty field should select option 0 (
Select UserName ). This would solve the saving null values as well.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top