locked
Operator '=' is not defined for type 'Integer' and type 'DBNull'. RRS feed

  • Question

  • User-1106823036 posted

    My scenario goes like this: I have sections (section1- section2-...)

    for each section there are classes(section1:class1- class2, section2:class1,...)

    I want to display for each section its own classes.

    here is my db structure:

    table: Sections                      table:classes
    
    
    Id | sections                        Id | sectionId | class
    -------------
     1 | section1                        1  |     1     | class1
     2 | section2                        2  |     1     | class2
                                         3  |     2     | class1

    here is what I am trying to do

     <%
                    Dim tb As SQLExec = New SQLExec
                    Dim sql As String
                    Dim DS As DataSet
                  
                    sql = "Select * from sections "
                   
                    sql += " Select * from classes"
                  
                    
                    
                
                          
                    DS = SQLExec.Cursor(sql)
                    
                    
                %>
    
     <%  If DS.Tables(0).Rows.Count > 0 Then
                                For i = 0 To DS.Tables(0).Rows.Count - 1
                        %>
    
                        <li><a href="#"><%=DS.Tables(0).Rows(i)("sections")%></a>
                            <ul>
                                <%  If DS.Tables(1).Rows.Count > 0 Then
    
                                        For i1 = 0 To DS.Tables(1).Rows.Count - 1
                                            If (DS.Tables(0).Rows(i)("id") = DS.Tables(1).Rows(i1)("sectionId")) Then
    
    
    
                                %>
    
                                <li><a href="#"><%=DS.Tables(1).Rows(i1)("class")%></a></li>
                                <% End If
                                        Next
                                    End If%>
                            </ul>
    
                        </li>
                        <% 
                                           Next
                                       End If%>

    It is throwing an exception at this statement

    If (DS.Tables(0).Rows(i)("id") = DS.Tables(1).Rows(i1)("sectionId")) Then

    This is the error

    Operator '=' is not defined for type 'Integer' and type 'DBNull'.

    can please someone explain to me what is going on and how can I fix the problem

    Thank you in advance

    Wednesday, January 27, 2016 10:37 AM

Answers

  • User-1716253493 posted

    Try this

    If Convert.ToInt32(DS.Tables(0).Rows(i)("id")) = Convert.ToInt32(DS.Tables(1).Rows(i1)("sectionId")) Then

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2016 1:12 AM
  • User-986267747 posted

    Hi lolo512,

    Operator '=' is not defined for type 'Integer' and type 'DBNull'.

    can please someone explain to me what is going on and how can I fix the problem

    According to your description, the problem is that the value you are trying to compare is DBnull, so you need to convert these value so that we could compare it with ohters form database. As eThunder say, you could use IsDBNull to solve your problem, i suggest that you could create a function and Pass the data in the database to the function so you can get the value you want to get.

     Private Function convertInteger(intInteger As Object) As Integer
    
            If IsDBNull(intInteger) Then
                Return 0
            End If
    
            Return intInteger
    
        End Function
    If (convertInteger(DS.Tables(0).Rows(i)("id"))= convertInteger(DS.Tables(1).Rows(i1)("sectionId"))) Then

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2016 5:57 AM

All replies

  • User1578460427 posted

    lolo512

    throwing an exception at this statement

    If (DS.Tables(0).Rows(i)("id") = DS.Tables(1).Rows(i1)("sectionId")) Then

    This is the error

    Operator '=' is not defined for type 'Integer' and type 'DBNull'.

    You need to check that each item in the If statement are not null using the IsDBNull function, then you can proceed to check whether both rows contain the the same id.

    See solution at: Operator '=' is not defined for type 'DBNull' and type 'Integer'

    Wednesday, January 27, 2016 11:44 AM
  • User-1716253493 posted

    Try this

    If Convert.ToInt32(DS.Tables(0).Rows(i)("id")) = Convert.ToInt32(DS.Tables(1).Rows(i1)("sectionId")) Then

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2016 1:12 AM
  • User-986267747 posted

    Hi lolo512,

    Operator '=' is not defined for type 'Integer' and type 'DBNull'.

    can please someone explain to me what is going on and how can I fix the problem

    According to your description, the problem is that the value you are trying to compare is DBnull, so you need to convert these value so that we could compare it with ohters form database. As eThunder say, you could use IsDBNull to solve your problem, i suggest that you could create a function and Pass the data in the database to the function so you can get the value you want to get.

     Private Function convertInteger(intInteger As Object) As Integer
    
            If IsDBNull(intInteger) Then
                Return 0
            End If
    
            Return intInteger
    
        End Function
    If (convertInteger(DS.Tables(0).Rows(i)("id"))= convertInteger(DS.Tables(1).Rows(i1)("sectionId"))) Then

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2016 5:57 AM