• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1255
  • Last Modified:

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

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
Mike Miller
Asked:
Mike Miller
  • 7
  • 6
1 Solution
 
Michael FowlerSolutions ConsultantCommented:
You can use something like this

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

Open in new window

0
 
Mike MillerSoftware EngineerAuthor Commented:
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
 
Michael FowlerSolutions ConsultantCommented:
<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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Mike MillerSoftware EngineerAuthor Commented:
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
 
Mike MillerSoftware EngineerAuthor Commented:
FYI I'm using vb.net and there is a blank value in the DDL it can default to when NULL
0
 
Michael FowlerSolutions ConsultantCommented:
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
 
Michael FowlerSolutions ConsultantCommented:
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
 
Mike MillerSoftware EngineerAuthor Commented:
Just thought of something...we can't use "Text" in a dropdownlist can we?
0
 
Michael FowlerSolutions ConsultantCommented:
0
 
Michael FowlerSolutions ConsultantCommented:
I cannot test at the moment but I thought your original code was working fine unless a null value was encountered
0
 
Mike MillerSoftware EngineerAuthor Commented:
I have not been successful yet, but am not abandoning this question
0
 
Mike MillerSoftware EngineerAuthor Commented:
Text='<%# Eval("ShipVia") == null ? "0" : Eval("ShipVia")%'>

Open in new window


gets

BC36637: The '?' character cannot be used here.
0
 
Michael FowlerSolutions ConsultantCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now