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 %>'>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With