Solved

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

Posted on 2009-03-30
27
836 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:Real_coffee
  • 13
  • 9
  • 4
  • +1
27 Comments
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
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.
0
 
LVL 7

Expert Comment

by:thiyaguk
Comment Utility
if you are going to just display the salesperson rather than selecting you can use
dropdownlist.Text = ""
0
 
LVL 1

Expert Comment

by:hari_tailor
Comment Utility
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>
0
 

Author Comment

by:Real_coffee
Comment Utility
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?
0
 

Author Comment

by:Real_coffee
Comment Utility
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?
0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
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.
0
 

Author Comment

by:Real_coffee
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:hari_tailor
Comment Utility
DropDownList5.SelectedValue = "John Donne" <example>
you need to define selected value from codebehind as above
0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
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.
0
 

Author Comment

by:Real_coffee
Comment Utility
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?


0
 

Author Comment

by:Real_coffee
Comment Utility
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?
0
 
LVL 1

Expert Comment

by:hari_tailor
Comment Utility
0
 

Author Comment

by:Real_coffee
Comment Utility
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?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Expert Comment

by:hari_tailor
Comment Utility
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
0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
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.
0
 

Author Comment

by:Real_coffee
Comment Utility
 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.
0
 

Author Comment

by:Real_coffee
Comment Utility
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

0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
Can you post the select command for the formview's datasource? And also the the select command for the sales_persons.
0
 

Author Comment

by:Real_coffee
Comment Utility
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?
0
 

Author Comment

by:Real_coffee
Comment Utility
oobayly - how about this -

********

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_22545473.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"


0
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
Comment Utility
Ah, so is sales_current being stored as a text field? You really should have a list of all the sales people in a table, with a status flag to specify whether they are an active sales person. This way you can populate the combobox dynamically.

The code below is an example of how to bind to a label & combobox using 2 datasources. I've used two tables that contains makes & models of vehicles.
  • The Model table tblFNMod contains a record fldFNMan which is the Manufacturer ID
  • The Manufactuer table tblFNMan contains fldFNMan & fldManDesc, and we only get active manufacturers (fldStat = 'E')
Hopefully you can follow it.
  • The formview binds to modSource, and provides the Model record, it selects not only the current manufacturer id, but also the name of the manufacturer. If the Manufacturer status is not 'E', then it uses a null Manufacturer ID
  • The combobox's datasource is set to manSource, and it's selected value is set the Manufacturer ID
  • Likewise the label's text is set to the Manufacturer name
Basically, your sales people table would be like my manufacturers table. You would also need to add an empty record to the sales people table, so as not to cause an exception when loading a record with an old sales person.


  <asp:FormView ID="FormView1" runat="server" DataSourceID="modSource">

    <ItemTemplate>

      <asp:Label ID="Label1" runat="server" Text='<%# Bind("fldFNMan") %>'></asp:Label>

      <asp:Label ID="lblCurrentMake" runat="server" Text='<%# Bind("fldManDesc") %>'></asp:Label>

      <br />

      <asp:DropDownList ID="comboMake" runat="server" DataSourceID="manSource" 

        DataTextField="fldManDesc" DataValueField="fldFNMan" SelectedValue='<%# Bind("fldFNMan") %>'>

      </asp:DropDownList>

    </ItemTemplate>

  </asp:FormView>

  <asp:SqlDataSource ID="manSource" runat="server" 

    SelectCommand="SELECT fldFNMan, fldManDesc FROM FleetNet.dbo.tblFNMan WHERE FleetNet.dbo.tblFNMan.fldStat = 'E'">

  </asp:SqlDataSource>

  <asp:SqlDataSource ID="modSource" runat="server" 

    SelectCommand="SELECT TOP 10

fldManDesc,

CASE FleetNet.dbo.tblFNMan.fldStat

  WHEN 'E' THEN FleetNet.dbo.tblFNMod.fldFNMan

  ELSE ''

END AS fldFNMan

FROM FleetNet.dbo.tblFNMod

INNER JOIN FleetNet.dbo.tblFNMan ON  FleetNet.dbo.tblFNMan.fldFNMan =  FleetNet.dbo.tblFNMod.fldFNMan">

  </asp:SqlDataSource>

Open in new window

0
 

Author Comment

by:Real_coffee
Comment Utility
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.
0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
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

0
 

Author Comment

by:Real_coffee
Comment Utility
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

0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
"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".




0
 

Author Closing Comment

by:Real_coffee
Comment Utility
Many Thanks for your help with this question
0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
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.
 
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now