ASP.NET .Bind() Issues on Selected Value of Drop Down List. How to handle Null Value

Hi guys,

I’ve got a dropdown list that’s being bound in a formview as part of an objectDataSource:

<asp:DropDownList ID="EmployeeDropDown" CssClass="input" runat="server" SelectedValue='<%# Bind("Employee") %>'
				  AppendDataBoundItems="True" DataSourceID="sqlEmployees" DataTextField="Name" DataValueField="Employee" />

This works fine, until there’s a value in the objectdatasource for Employee that is DBNull. Then I get an exception as there’s not null user in the dropdown list.

I’m trying to:

<asp:DropDownList ID="EmployeeDropDown" CssClass="input" runat="server" SelectedValue='<%# IIf(Bind("Employee") = System.DBNull.Value, "0", Bind("Employee") %>'
				  AppendDataBoundItems="True" DataSourceID="sqlEmployees" DataTextField="Name" DataValueField="Employee" />

But the bind function fails, and I get a “Bind is not declared.” error. What’s the correct way of handling dbnull’s when binding from an objectdatasource.


Change the Bind(“Employee”) to Eval(“Employee”)

Thanks for your reply, dhtmlgod.

Eval would work, but I’m trying to bind this dropdown two ways. So the selected value is chosen from the database, and then the new value updated in the table record on submit. My understanding of Eval was that it was one way??

Thanks in advance!

The best options might be to not return null (pah, I really don’t like nulls!), instead return -1…


In my BLL (using the MS Example from tutorials), I’ve got a get method which pulls a datatable (one row in fact) by a particular ID. You reckon a check in there might be the best place to do it.

I guess I’m just trying to figure out a way to intercept the process of the data being pulled into my datatable -> objectdatasource -> formview -> dropdownlist checkign for null’s and giving it a safe value.

I’ll try this first thing in the morning.

I generally set a default value for any columns that might have a null, if it expects an integer, I’ll generally use -1. But there are a number of ways, using the SQL Server ISNULL() function is another.

Thankyou dthmlgod!

That appears to be working. I’ll remember it for next time, doing error/value checking on retrieval as well as when saving data!

I changed my retrieve into:

 <System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, False)> Public Function GetDataByAssetID(ByVal AssetID As Integer) As AssetRegister.AssetsDataTable
	Dim DT As New AssetRegister.AssetsDataTable
	DT = Adapter.GetDataByAssetID(AssetID)

	If DT.Rows(0).Item("Employee") Is System.DBNull.Value Then
	  DT.Rows(0).Item("Employee") = "Select"
	End If

	Return DT
  End Function