Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for null, inline, on an ASP.net Bind() call

I've got a bit of code that I started to manage, and it's begun to fail due to some data missing in the database. This case could happen in the future, so I'd like to gracefully handle the nulls in the front end.

Here's the current code:

<asp:DropDownList ID="ddlContact" runat="server"
  SelectedIndex='<%# Bind("contactInfo") == null  ? "" : Bind("contactInfo") %>'>

It doesn't seem to have any affect on it, and the page still throws a NullReferenceException. It needs to be a Bind() due to the two-way data binding requirement, so I can't use Eval(). Any ideas?

I've tried to use the null-coallescing operator "??" but that gives me a compilation error stating that Bind() does not exist in the current context. That could would look like this:

<asp:DropDownList ID="ddlContact" runat="server"
  SelectedIndex='<%# Bind("contactInfo") ?? string.Empty %>'>
like image 288
Carl Avatar asked Sep 01 '10 19:09

Carl


1 Answers

Check this one:

Bind NULL

This one should give you more ideas:

How to handle null values in DataBinder.Eval()

Handling Null Database Values Using Data Source Controls

When the AppendDataBoundItems property is set to true, the DropDownList control is populated with both static items and data generated from the data source. The static list item that is added to the DropDownList control has the Value property set to an empty string. With that, a data item that contains a null value is bound to the static list item.

like image 134
Leniel Maccaferri Avatar answered Sep 25 '22 01:09

Leniel Maccaferri