Testing for null, inline, calling ASP.net Bind ()

I have some code that I started to manage, and it started to crash due to some data missing from the database. This case may happen in the future, so I would like to gracefully handle zeros in the interface.

Here's the current code:

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

This does not seem to affect it, and the page still throws a NullReferenceException. It must be Bind () due to the two-way data binding requirement, so I cannot use Eval (). Any ideas?

I tried using the null collision operator "??" but this gives me a compilation error that states that Bind () does not exist in the current context. It might look like this:

 <asp:DropDownList ID="ddlContact" runat="server" SelectedIndex='<%# Bind("contactInfo") ?? string.Empty %>'> 
+7
null drop-down-menu data-binding bind
source share
1 answer

Check this:

Bind NULL

This should give you more ideas:

How to handle null values ​​in DataBinder.Eval ()

Handling Database Zero Using Data Source Controls

When the AppendDataBoundItems property is set to true, the DropDownList control is populated with both static elements and data obtained from the data source. The static list item that is added to the DropDownList control. the Value property is set to an empty string. In this case, a data item that contains a null value tied to a static list item.

+2
source share

All Articles