views:

84

answers:

2

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 %>'>
+1  A: 

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 Macaferi
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
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
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 Macaferi
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
Carl: can't you treat the values from the SQL query, that is, replace null for string empty in the database?
Leniel Macaferi
Check the updated answer with a link to MSDN.
Leniel Macaferi
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
A: 

Now that I look again your error Bind() does not exist in the current context I think that the problem is that you can not find this parameter on your select command !

You need to checked with the DBNull (and not with the null)

http://msdn.microsoft.com/en-us/library/system.dbnull.aspx

Aristos
I've tried comparing to System.DBNull.Value, and that didn't seem to fix it.
Carl
@Carl I have update my answer, finally I think that the problem is that you cant find this parametre on select ! and the null is not the problem
Aristos
The error "Bind() does not exist in the current context" only appears when I put parenthesis around the Bind() statement. Otherwise, it appears to work for other datasets where the particular value is not null.
Carl