Solved

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

Posted on 2014-11-13
13
888 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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