Solved

selectedValue property in dropdownlist of detailsview??

Posted on 2008-10-20
8
585 Views
Last Modified: 2012-05-05
Hi,

I am having a little problem with the NULL and emtpy value of the records from the database.
I am trying to bind a dropdownlist to the detailsview in edit mode.
I am getting the selectedvalue is not in the list item of values.

The dropdownlist  values come from dstatus field of table1(id,dstatus).
The dstatus field has the following values (active,inactive,pass,fail).
the values of detailsview come from this table called table2(id,name,status)

I want to put a dropdown in edit mode of detailsview.

The status field contains both null and empty values including active,inactive,pass,fail.
How do I need to handle the null and empty?

<asp:DropDownList ID="DropDown1" runat="server" SelectedValue='<%# Bind("status") %>'
DataValueField="dstatus" DataTextField="dstatus" AppendDataBoundItems="True" DataSourceID="DSdStatus">
<asp:ListItem value=''></asp:ListItem>
</asp:DropDownList>

The Dropdownlist basically has the following values:active,inactive,pass,fail.
The values of status in the table2 are active,inactive,pass,fail, NULL and EMPTY.
Since NULL and EMPTY values are not in the dropdownlist, it is erroring out.
How do I need to handle this??
0
Comment
Question by:dkim18
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 22762548
How are you filling the Datasource?

Your Datasource needs to include a record for all possible selectedvalues, including null.  I generally do this using a union query.


<asp:SqlDatasource Id=DSdStatus  Runat=Server

  ...

  SelectCommand="Select t.id, t.status from table2 t union select null as id, '-select-' as status order by status;"

  ></asp:SqlDatasource>

Open in new window

0
 

Author Comment

by:dkim18
ID: 22762779
OK.
Thanks for that. I will try that.

Funny thing is I am getting the error from when status field has a value.
Say, the status field has a value of 'active' and I would like to edit it so I click the edit link. Then it errors out saying that the selectedvalue is invalid because it doesn't exit in the list of items.

I thought I got this dropdownlist in detailsview in edit mode all working.
Today I was testing and I am getting this error and I don't know what problem is.
I looked at it all day today.
0
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 22762921
Both your DataValueField and DataTextField are dstatus.  How are your tables linked?  Is it not on table2.status=table1.id?  If so, your DataValueField would need to be the id key.
0
 

Author Comment

by:dkim18
ID: 22767122
Hi,

I am using the table1 as a general lookup table for my dropdowns.
Mine is pretty simple table so I haven't linked them.
I am just storing the value in the table 2 like table2.status = table1.dstatus.

I don't know understand why it is saying the selectedvalue is not in the list of items...
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:dkim18
ID: 22768007
I just cannot figure this out.
SelectedValue is one of the list items but it is telling me that it is not one of the items.

When edit on the empty status, it loads the dropdown OK.
But when the value is in that field, the selectedvalue comes out invalid.

What gives?? It is really frustrating. It shouldn't be this difficult to put a simple dropdownlist.
0
 

Author Comment

by:dkim18
ID: 22769566
I am so despair...so I am trying out everything.
Someone said to place rtrim().
It seems to be working for me now but I need to do more testing if this really fixed it.

I don't  get it though. I checked the both fields from two tables.
They are same length and same data type.

Any idea why would it cause the problem.
0
 
LVL 16

Accepted Solution

by:
GreymanMSC earned 500 total points
ID: 22772343
Exactly what data type are the fields?  char, varchar, or nvarchar?  It makes a difference.

It sounds as though you are using char, which is a fixed length string.  Thus additional spaces are stored in the field.  But the dropdownlist trims the strings it uses for vales, so the databound string is longer than the supposedly matching value.

Use varchar,
0
 

Author Comment

by:dkim18
ID: 22775682
All my fields are in varchar already.
So that reason doesn't apply to my issue. So I don't understand what's really happening. It is killing me.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

12 Experts available now in Live!

Get 1:1 Help Now