Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid displaying NULL value gives error 2

Status
Not open for further replies.

clanm

Programmer
Dec 26, 2005
237
US
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!
 
Well one way to accomplish this wo8uld be to do in your SQL code:
Code:
select
      some field,
      case when somefield2 is not null then somefield2 else 'Z'

then in your function check for the value "Z":
Code:
Function DisplayStatus(ByVal WkPkgStatus As Object) As String
        Select Case WkPkgStatus
            Case "F"
                Return "Funded"
            Case "U"
                Return "UnFunded"
            Case "R"
                Return "RollUp"
            Case "Z"
                Return "Not Set"
            Case Else
                Return "Not Set"
        End Select
End Function

Hope this helps
 
You shouldn't have to edit your SQL like that (what happens if "Z" becomes a valid entry for that field?). Instead, in your function check if the value equals DBNull before proceeding.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm / PsychoCoder!

thanks, that did the trick!


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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top