Solved

selectedValue property in dropdownlist of detailsview??

Posted on 2008-10-20
8
603 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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
 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Setting up a IIS 8 Web Server to send and receive XML files 7 125
Where is this file? 3 42
c#, datatable, aspx 4 53
CSS for Popup in ASP.NET 4 49
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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