Solved

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

Posted on 2014-11-13
13
707 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:mwmiller78
  • 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:mwmiller78
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
 

Author Comment

by:mwmiller78
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:mwmiller78
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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:mwmiller78
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:mwmiller78
ID: 40471827
I have not been successful yet, but am not abandoning this question
0
 

Author Comment

by:mwmiller78
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

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

18 Experts available now in Live!

Get 1:1 Help Now