2010-04-12 3 views
1

pouvez-vous s'il vous plaît m'aider à trouver la raison du mystère que j'ai trouvé? Dans le code ci-dessous, je crée un DataTable et le filtre. Quand j'utilise filter1, tout fonctionne comme prévu. Lorsque j'utilise filter2, tout fonctionne comme prévu uniquement si la variable SubsectionAmount est inférieure à 10. Dès que j'ai défini SubsectionAmount = 10, le tableau dr2 renvoie Nothing. Je ne peux pas trouver ce qui ne va pas. Voici le code:DataTable Filter mystère

Imports System.Data

classe partielle FilterTest Hérite 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

Répondre

1

La ligne

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

me semble douteux (?)

Tenir compte ce bout de code:

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

lors de l'impression j vous obtiendrez "Hello21" et non "hello3". L'opérateur + appliqué sur une chaîne acceptera n'importe quel objet du côté droit et les utilisera en appelant ToString() sur l'objet, ce qui rendra votre int effectivement une chaîne. Maintenant, je suppose que dans VB.Net, il est assez similaire, ce qui peut ne pas être ce que vous voulez.

Mise à jour Apparemment, VB.Net fait les choses différemment, si heureusement ignorer ...

+0

Je pensais que cela aussi, mais juste testé, et ce code fonctionne comme prévu: Dim As String = de "Bibble" & x + 1 produit bibble4 lorsque x = 3 – NibblyPig

+0

Je l'ai utilisé à la fois "section =" & SectionID & "AND" & "Subsection =" & (SubsectionID + 1) et même "Section =" & SectionID & "AND" & "Subsection = 2". Il a produit le même mystère: ne peut pas filtrer le datatable. Plus de pensées s'il vous plaît? – user283897

1

changer votre colonne ajouter des déclarations à ce qui suit il fait les comparaisons correctement.

.Columns.Add("Section", GetType(Integer)) 
    .Columns.Add("Subsection", GetType(Integer)) 
    .Columns.Add("FieldString") 
+0

ou changez vos filtres pour cela filter1 = String.Format ("Section = '{0}' AND Subsection = '{1}'", SectionID, SubsectionID) filter2 = Chaîne.Format ("Section = '{0 } 'AND Subsection =' {1} '", SectionID, SubsectionID + 1) –

+0

Salut gbogumil, Merci beaucoup. Vos deux suggestions fonctionnent! – user283897