7

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 %>'>
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Carl
  • 1,706
  • 1
  • 17
  • 25

1 Answers1

2

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.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • Thanks, I'd seen that second link, but did not find what I was looking for. It dealt mostly with Eval() and not Bind(), and for some reason, there's a difference in how nulls are handled between them. – Carl Sep 01 '10 at 19:41
  • As far as the first link, as soon as I surrounded Bind in parenthesis, I got the "Bind does not exist in this context" compilation error. So strange. – Carl Sep 01 '10 at 19:41
  • Carl: I think you're not doing it the right way. You should treat the value that is bound using something like an event method in the code-behind page. There you can check if the value of contactInfo is null and then assign a proper value for SelectedIndex. – Leniel Maccaferri Sep 02 '10 at 00:13
  • Leniel, I would agree, but at the moment, I cannot change the code behind. This code was from a legacy project that was given to me, and code behind changes are being frowned upon by others at the moment. I was hoping I could do a quick null-check in the aspx page, and be done with it. Thank you for the input. I too want to gather the data in the code-behind, sanitize it if I need to, and then present it in the aspx page. – Carl Sep 02 '10 at 15:45
  • Carl: can't you treat the values from the SQL query, that is, replace null for string empty in the database? – Leniel Maccaferri Sep 02 '10 at 17:14
  • Leniel, that's what I ended up doing. I modified the sql query output to return an empty string, instead of null. I don't like the solution, but it works, so I can't hate it ;-) – Carl Sep 07 '10 at 12:43