?
Solved

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

Posted on 2014-11-13
13
Medium Priority
?
1,013 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:Michael Fowler
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:Michael Fowler
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:Michael Fowler
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:Michael Fowler
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:Michael Fowler
ID: 40441799
0
 
LVL 23

Expert Comment

by:Michael Fowler
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:
Michael Fowler earned 2000 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

Industry Leaders: 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!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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