Solved

ASP.NET - Handling NULL values when using EVAL in a dropdownlist

Posted on 2014-11-13
13
837 Views
Last Modified: 2014-12-17
I am selecting a value from a dropdown using this

<asp:DropDownList ID="ddlShipVia" runat="server" Width="180" Font-Names="Verdana" ForeColor="#666666" Height="21" Style="font-size:12px;" Text=<%#Eval("ShipVia")%>></asp:DropDownList>

Open in new window


Works great except when the DB value is NULL. How do I handle this?
0
Comment
Question by:Mike Miller
  • 7
  • 6
13 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40441734
You can use something like this

Eval("ShipVia") == null?"0": Eval("ShipVia");

Open in new window

0
 

Author Comment

by:Mike Miller
ID: 40441744
How? Where?

<asp:DropDownList ID="ddlShipVia" runat="server" Width="180" Font-Names="Verdana" ForeColor="#666666" Height="21" Style="font-size:12px;" Text=Eval("ShipVia") == null?"0": Eval("ShipVia");></asp:DropDownList>

Open in new window

0
 
LVL 23

Expert Comment

by:Michael74
ID: 40441750
<asp:DropDownList ID="ddlShipVia" runat="server" Width="180" Font-Names="Verdana" ForeColor="#666666" Height="21" Style="font-size:12px;" Text=<%#Eval("ShipVia") == null?"0": Eval("ShipVia")%>></asp:DropDownList>

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Mike Miller
ID: 40441758
Playing around with it but here's what your code gets me...

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30201: Expression expected.

Source Error:

Line 428:</td>
Line 429:<td align = "left" valign="top">
Line 430:<asp:DropDownList ID="ddlShipVia" runat="server" Width="180" Font-Names="Verdana" ForeColor="#666666" Height="21" Style="font-size:12px;" Text=<%#Eval("ShipVia") == null?"0": Eval("ShipVia")%>></asp:DropDownList>
Line 431:</td>
Line 432:</tr>
0
 

Author Comment

by:Mike Miller
ID: 40441765
FYI I'm using vb.net and there is a blank value in the DDL it can default to when NULL
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40441769
Sorry I do not have access to VS at the moment for my own testing. Try this
<asp:DropDownList ID="ddlShipVia" runat="server" Width="180" Font-Names="Verdana" ForeColor="#666666" Height="21" Style="font-size:12px;" Text='<%# Eval("ShipVia") == null ? "0" : Eval("ShipVia")%'>></asp:DropDownList>

Open in new window

0
 
LVL 23

Expert Comment

by:Michael74
ID: 40441775
OK this code returns 0 when the value is null. When it is working just replace "0" with the appropiate value eg an empty string ""
0
 

Author Comment

by:Mike Miller
ID: 40441790
Just thought of something...we can't use "Text" in a dropdownlist can we?
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40441799
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40441800
I cannot test at the moment but I thought your original code was working fine unless a null value was encountered
0
 

Author Comment

by:Mike Miller
ID: 40471827
I have not been successful yet, but am not abandoning this question
0
 

Author Comment

by:Mike Miller
ID: 40471859
Text='<%# Eval("ShipVia") == null ? "0" : Eval("ShipVia")%'>

Open in new window


gets

BC36637: The '?' character cannot be used here.
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40472809
The expression is an inline if statement and so the "?" should be valid

http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

Another option here would be to use code behind to achieve the same result
public string ProcessMyDataItem(object value)
{
  if (value== null)
  {
     return "";
  }
  return value.ToString();
}

Open in new window


then in the page use
Text='<%# ProcessMyDataItem(Eval("ShipVia")) %>'

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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