tags:

views:

28

answers:

2

Hi, could you please help me find the reason of the mystery I've found? In the below code, I create a DataTable and filter it. When I use filter1, everything works as expected. When I use filter2, everything works as expected only if the SubsectionAmount variable is less than 10. As soon as I set SubsectionAmount=10, the dr2 array returns Nothing. I can't find what is wrong. Here is the code:

Imports System.Data

Partial Class FilterTest Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Call FilterTable()
End Sub

Sub FilterTable()
    Dim dtSubsections As New DataTable
    Dim SectionID As Integer, SubsectionID As Integer
    Dim SubsectionAmount As Integer
    Dim filter1 As String, filter2 As String
    Dim rowID As Integer
    Dim dr1() As DataRow, dr2() As DataRow

    With dtSubsections
        .Columns.Add("Section")
        .Columns.Add("Subsection")
        .Columns.Add("FieldString")

        SectionID = 1
        SubsectionAmount = 10 '9
        For SubsectionID = 1 To SubsectionAmount
            .Rows.Add(SectionID, SubsectionID, "abcd" & CStr(SubsectionID))
        Next SubsectionID

        For rowID = 0 To .Rows.Count - 1
            Response.Write(.Rows(rowID).Item(0).ToString & " " _
                           & .Rows(rowID).Item(1).ToString & " " _
                           & .Rows(rowID).Item(2).ToString & "<BR>")
        Next
        SubsectionID = 1
        filter1 = "Section=" & SectionID & " AND " & "Subsection=" & SubsectionID
        filter2 = "Section=" & SectionID & " AND " & "Subsection=" & SubsectionID + 1

        dr1 = .Select(filter1)
        dr2 = .Select(filter2)

        Response.Write(dr1.Length & "<BR>")
        Response.Write(dr2.Length & "<BR>")
        If dr1.Length > 0 Then
            Response.Write(dr1(0).Item("FieldString").ToString & "<BR>")
        End If
        If dr2.Length > 0 Then
            Response.Write(dr2(0).Item("FieldString").ToString & "<BR>")
        End If
    End With
End Sub

End Class

+1  A: 

The line

"Section=" & SectionID & " AND " & "Subsection=" & SubsectionID + 1

looks dodgy to me (?)

Consider this snippet of code:

var i = 2;
string j = "Hello " + i + 1;

when you print j you will get "Hello21" and not "Hello3". The + operator applied on a string will accept any object on the right-hand side and uses them by calling ToString() on the object, hence making your int effectively a string. Now, I assume that in VB.Net it is quite similar, which may not be what you want.

Update Apparently VB.Net does things differently, so happily ignore...

flq
SLC
+1  A: 

change your column add statements to the following so it does the comparisons correctly.

    .Columns.Add("Section", GetType(Integer))
    .Columns.Add("Subsection", GetType(Integer))
    .Columns.Add("FieldString")
gbogumil
or change your filters to this filter1 = String.Format("Section='{0}' AND Subsection='{1}'", SectionID, SubsectionID) filter2 = String.Format("Section='{0}' AND Subsection='{1}'", SectionID, SubsectionID + 1)
gbogumil
Hi gbogumil, Thank you very much. Your both suggestions work!