Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-03-30
27
Medium Priority
?
849 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 9
  • 4
  • +1
27 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24017408
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
ID: 24017416
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
ID: 24017439
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Real_coffee
ID: 24017458
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
ID: 24017494
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
ID: 24017498
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
ID: 24017531
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
ID: 24017573
DropDownList5.SelectedValue = "John Donne" <example>
you need to define selected value from codebehind as above
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24017693
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
ID: 24017743
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
ID: 24017793
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
ID: 24017882
0
 

Author Comment

by:Real_coffee
ID: 24017964
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
 
LVL 1

Expert Comment

by:hari_tailor
ID: 24018001
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
ID: 24018067
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
ID: 24018270
 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
ID: 24020633
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
ID: 24020817
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
ID: 24020895
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
ID: 24021188
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 2000 total points
ID: 24021211
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
ID: 24021584
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
ID: 24021813
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
ID: 24022238
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
ID: 24022903
"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
ID: 31564263
Many Thanks for your help with this question
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24047922
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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