Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Datagrid handling NULL value gives error

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2006-09-15 : 11:27:10
I have a datagrid, and am having trouble when the value is NULL.
When I run the page I get:
Operator is not valid for type 'DBNull' and string "F".

When the code hit's a value of NULL in the database, the error occurs. If the value in the table is "F", then I get "Funded"...which is correct.

I've tried several things to handle the NULL value, but can't get around the error.

Here's the HTML:
<asp:Templatecolumn HeaderText="Status">
<ItemTemplate>
<%# DisplayStatus(DataBinder.Eval(Container.DataItem, "WkPkgStatus"))%>
</ItemTemplate>
</asp:Templatecolumn>

Here's the code behind:
Function DisplayStatus(ByVal WkPkgStatus As Object) As String
Select Case WkPkgStatus
Case "F"
Return "Funded"
Case "U"
Return "UnFunded"
Case "R"
Return "RollUp"
Case IsDBNull(WkPkgStatus.Value)
Return "Not Set"
Case Else
Return "Not Set"
End Select
End Function

Thanks!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-15 : 11:48:11
Function DisplayStatus(ByVal WkPkgStatus As Object) As String
If WkPkgStatus = "F" Then
Return "Funded"
ElseIf WkPkgStatus = "U" Then
Return "UnFunded"
ElseIf WkPkgStatus = "R" Then
Return "RollUp"
ElseIf IsDBNull(WkPkgStatus.Value) Then
Return "Not Set"
Else
Return "Not Set"
End If
End Function

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2006-09-15 : 15:54:55
Peso, thanks for the reply.

I used this, and it worked:
Function DisplayStatus(ByVal WkPkgStatus As Object) As String
If Not IsDBNull(WkPkgStatus) Then
Select Case WkPkgStatus
Case "F"
Return "Funded"
Case "U"
Return "UnFunded"
Case "R"
Return "RollUp"
Case Else
Return "Not Set"
End Select
Else
Return "Not Set"
End If
End Function
Go to Top of Page
   

- Advertisement -