I have an ASP.NET control that I have bound to a SQL result:
<asp:GridView ID="EmployeeSearchResults" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# (EmployeeSearchStatus(Eval("SeparationDate"),Eval("PositionTitle"),Eval("EffectiveDate"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
My EmployeeSearchStatus function is very basic, testing the values passed in for NULL, and creating a string to display:
Public Function EmployeeSearchStatus(ByVal SeparationDate As Object, ByVal PositionTitle As Object, ByVal EffectiveDate As Object) As String
Dim ReturnString As String = ""
If IsDBNull(SeparationDate) Then
ReturnString = "Currently Employed as "
Else
ReturnString = "Last Employed as "
End If
ReturnString += PositionTitle
If IsDBNull(SeparationDate) Then
ReturnString += " (effective " + EffectiveDate + ")."
Else
ReturnString += " (separated on " + SeparationDate + ")."
End If
Return ReturnString
End Function
Is this the proper way to handle NULL values coming back from SQL to an ASP.NET control? Is there a better technique?
Thanks,
Russell
Your code looks fine, although you could possibly simplify it a bit if you wanted:
The code does assume, however, that
PositionTitleandEffectiveDatewill never benull. If this is not enforced in the database, you could add some checks to the code to deal with these situations.