Link to home
Start Free TrialLog in
Avatar of Real_coffee
Real_coffee

asked on

DropDownList - has a SelectedValue which is invalid because it doesnt appear in the list of items.

Experts Im stuck with a dropdownlist - I know it must be easy but I cant figure it out.

I have a dropdownlist (in a formview) with a list of our current sales people. It populates the field: sales_current.
This is fine until the record contains a sales person who has left and doesnt appear in my list. The drop down then throws this error:

'DropDownList5 has a SelectedValue Which is invalid because it doesnt appear in the list of items.'

Which makes sense really, but how do I work around this?

I dont want to list all the sales people that have left in my list.


                           <asp:DropDownList ID="DropDownList5" runat="server"
                                SelectedValue='<%# Bind("sales_current") %>'>
                                <asp:ListItem Value="">-Please Select-</asp:ListItem>
                                <asp:ListItem>John Donne</asp:ListItem>
                                <asp:ListItem>Miss Smith</asp:ListItem>
                                <asp:ListItem>Mr Brown</asp:ListItem>
                                </asp:DropDownList>

Please help
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

Either you'll have to filter out records where the sales person has left, or populate the DropDown list, and validate the changes so an invalid sales person can't be selected.
if you are going to just display the salesperson rather than selecting you can use
dropdownlist.Text = ""
Avatar of hari_tailor
hari_tailor

Syntax of Dropdown list is
            <asp:DropDownList ID="DropDownList5" runat="server">
            <asp:ListItem Value="" Selected="True">Please Select</asp:ListItem>
            <asp:ListItem>John Donne</asp:ListItem>
            <asp:ListItem>Miss Smith</asp:ListItem>
            <asp:ListItem>Mr Brown</asp:ListItem>
            </asp:DropDownList>
Avatar of Real_coffee

ASKER

Cant filter out the leavers as current sales people 'take over' customer records of salespeople who have left.

Cant populate the dropdown with all the leavers (there are tons) and it doesn't make sense to the user - "hey why are all these leavers on here?"

I can see this question gets asked a lot but I have not found a solution as yet. Is there a way to programmatically add the the leaver to the list if the name isnt on there already? Maybe on the PreRender of the form or dropdownlist?
hari_taylor / thiyaquk - I have not explained clearly enough:

sometimes there will be a record with the sales_current having a value of maybe "Anne Brown" or "Joe Green" or many other names. My dropdown throws an exception because these names are not in my list of current salespeople in the dropdown.

I need the option to overwrite these old names with my dropdown of current users. If I need to add the original salesperson to the dropdown programmatically then thats ok (I dont know how to do it though) but I cant add them all in as a huge list.

am I making sense?
Fair enough, I thought that'd be the case. Another possible way is to manipulate the sales_current field when it's returned by the database server so that sales_current is set to an empty string if the sales person has left. That way, when the record is selected, it'll will immediately prompt the user to select a current sales person.
oobayly - no offense but they need to see the old salesperson name before they overwrite it with the new name (nice approach though).

hari_taylor - tried your code and it doesn't throw an exception but selected value doesn't get stored? it just stores a blank value each time.
DropDownList5.SelectedValue = "John Donne" <example>
you need to define selected value from codebehind as above
None taken.
I'm afraid the only other solutions I can offer are nasty cludges that require an readonly field to display the previous sales person, a conditional column added to the datasource, and separate datasources for the dropdown and the readonly field, specifying current sales people and all sales people respectively.
It's not ideal, and I'm borderline loath to even suggest it.
Hary_taylor - Maybe Im having a bad day but i cant see how to do that. Could you demonstrate for me?

oobayly - I was also thinking of a label with the value of the old person (maybe set to visible = false, some script reads this value and adds it into the dropdown before it gets rendered? Is that viable?


everybody -

Is the answer 2 fields? a readonly textbox and a dropdownlist next to it? You make the selection from the dropdown and it passes this to the value of the readonly textbox?
hari_taylor - I can only just follow this. (Im new to asp.net and I dont know any C#)

Could you possible show me a vb version of that behind code and show me where to put it?
Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        CType(fv.FindControl("DropDownList5"), DropDownList).SelectedValue = "John Donne"
End Sub
A useful tool to translate c# to vb and vice-versa. I use it all the time when I can't be bothered to do it by hand:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
In fact somebody's posted the vb version already in the comments.

@hari_tailor:
Just looking through it, I can't see why the example you posted would behave differently from the OP's problem. I haven't tested it, but surely there'll be some issues if the departments table returned only has active departments, and the teachers table references any department.
Apologies if I'm incorrect here.
 Protected Sub formview1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
        CType(FormView1.FindControl("DropDownList5"), DropDownList).SelectedValue = "John Donne"
    End Sub

This gives me the following - 'Object reference not set to an instance of an object'.

Im not sure that this is what I want as it looks as though it will send a selected value to the dropdownlist but what I want to do is use your dropdown to populate my field? your dropdown code worked fine - it didnt throw any exceptions but it didnt store the selected value to the field as it wasnt bound to anything.
Experts - I'm lost here.
Experts - I think my inability with this must be quite frustrating. Sorry.

hari_taylor - I cannot make that work. I might just not be up to that level. Thanks for the translation from C though.
oobayly - Thanks for your input so far.

If my original question cant be done easily then can someone show me how to populate a read only text box with the selected value from a dropdown both from within the same formview? has to be in VB.

Really - I must get it done this evening. Happy to increase he points

Can you post the select command for the formview's datasource? And also the the select command for the sales_persons.
SelectCommand="SELECT * FROM [Clients] WHERE ([RECORD_ID] = @RECORD_ID)"
            CancelSelectOnNullParameter="False"
            <SelectParameters>
                  <asp:SessionParameter Name="RECORD_ID" SessionField="record_id" Type="Int32"
                    DefaultValue="1" />

There is no select command for the sales people - the dropdown is a manually entered list box.

I can create a fresh little page that shows the issue if you like?
oobayly - how about this -

********

https://www.experts-exchange.com/questions/22545473/asp-DropDownList-databound-value-not-in-list-trap-error.html

You have the DropDownList's SelectedValue property bound to one of the FormView's fields, right?
You can put another helper function in between that checks if the DDL list contains the value.

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsSomething"
                    DataTextField="Name" DataValueField="ID" SelectedValue='<%# CheckIfValueIsPresent("Category") %>' >

then in the CheckIfValueIsPresent check whether the DDL contains the selected item :)
*********
Oobayley - Could we use this and in the checkifvalueispresent function insert the value into our drop down if it isnt present?

I have no idea how to do this before you say -  "sure do that"


ASKER CERTIFIED SOLUTION
Avatar of oobayly
oobayly
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes sales_current is the name of the current sales person, its a text field.

I think In understand your code - it doesnt crash because you have ALL manufacturers in your fleetnet table. However you only display the ones marked 'E' in your dropdown. I guess E is for Extant or still exists.

I'll give it a try.
Exactly, I suggest this change for the manSource SELECT command. This will mean it will show manufacturers that still exist and the record with an empty ID. It means that the record with the empty ID isn't flagged as still existing.
To be honest, I never do this as I do all my databinding manually so I can manipulate the datasource and add the null records at runtime. It saves cluttering the database.
SELECT fldFNMan, fldManDesc FROM FleetNet.dbo.tblFNMan WHERE FleetNet.dbo.tblFNMan.fldStat = 'E' OR FleetNet.dbo.tblFNMan.fldFNMan = ''

Open in new window

I understand the approach and frankly its as near as I am ever going to get to what I wanted. So a big Thankyou for that.

But having spent a whole day on the problem with no coding success (yet), this is what I think I was really after. I just dont have the skill to make it work:

If you have time Id like your opinion on this as I think its a generic sort of approach that could prove useful for me in the future:

Assumptions:
I will have a table called 'salespeople' that contains a single field 'salesperson', which is the name of the salesperson.

I am populating a field called 'sales_current' within a table called 'clients' with the result of a dropdown that is populated by the contents of 'salesperson' from table 'salespeople'.

Sometimes sales_current will contain a value that is not held in salespeople.

'first Id need a dropdownlist that checks itself;

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsSalespeople"
  DataTextField="current_sales" DataValueField="salesperson" SelectedValue='<%# CheckIfValueIsPresent("salesperson") %>' >


'then the function that checks if the value exists in the dropdown;

function CheckIfValueIsPresent as string

dropdownList1.Items.FindByValue(dropdownValuehere)

' then an if / endif to see if the value is present in the dropdown list. If not then insert it

dropdownlist1.items.insert(0, new listitem("Something here", "0"))

end function

What do you think oobayly? Am I talking nonsense here?

Real_Coffee

"I will have a table called 'salespeople' that contains a single field 'salesperson', which is the name of the salesperson."
No, it will have something like this:
  • person_id int primary key not null
  • person_name varchar not null
  • person_active bit not null
"Sometimes sales_current will contain a value that is not held in salespeople."
The sales_current will be an number that refers to the salespeople table, and it will always contains a person_id. It's just that that ID may have it's active flag set to false.

"' then an if / endif to see if the value is present in the dropdown list. If not then insert it"
Unfortunately, when your using databinding, you can't add items directly to the combobox, as it's building it's list from the datasource.
What you could do is check if the value is in the list, and if it isn't return the empty value, so the selected item is the one that says "Select a Sales Person".




Many Thanks for your help with this question
Addendum:
"Unfortunately, when your using databinding, you can't add items directly to the combobox, as it's building it's list from the datasource."
I was incorrect. You can hook the DataBound event, and use that to add extra items to the items created from the databinding.

I'm glad the example helped.