DataTable filter expression issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wbosw
    New Member
    • Mar 2007
    • 36

    DataTable filter expression issue

    I'm trying to filter the datatable at runtime from data selected from dropdown listboxes and a texbox. I place the values into variables and use the variables in the filter expression. However, this does not work even though the variavbles contain the right values. See the following code.

    Dim x As String = Me.ddlSearchSki ll1.SelectedVal ue
    Dim y As String = Me.ddlSkillLeve l1.SelectedItem .Text
    Dim w As String = Me.txtExp1.Text .ToString

    Dim DataRow2 As DataRow()

    DataRow2 = dt.Select("Skil lsId = ' x ' AND SkillLevel > ' y ' AND
    MonthsExperienc e > ' w ' ", "Name DESC")


    When I hard code the values the expression works(SEE BELOW). Any suggestions on fixing this issue?

    DataRow2 = dt.Select("Skil lsId = ' 1 ' AND SkillLevel > ' 1 ' AND
    MonthsExperienc e > ' 1' ", "Name DESC")


    Thanks,
    Wbosw
  • mikeyeli
    New Member
    • Oct 2007
    • 63

    #2
    I dont see anywhere on that code where you replace the x and y from that string with the filter values, i think you missed it.
    Anyways you can use String.Format for that

    try:

    VB:
    Code:
    Dim x = "algunId"
    Dim y = "algunCosoMes"
    Dim filtroDeQuery = "SkillsId = ' {0} ' AND SkillLevel > ' y ' AND MonthsExperience > ' {1} ' "
    filtroDeQuery = String.Format(filtroDeQuery, x, y)
    
    DataRow2 = dt.Select(filtroDeQuery, "Name DESC")
    it should work.

    Comment

    • GayathriP
      New Member
      • Oct 2007
      • 17

      #3
      Just a small change in your code will work fine.

      DataRow2 = dt.Select("Skil lsId = '" & x ?& "' AND SkillLevel > '" & y & "' AND
      MonthsExperienc e > ' " & w & "' ", "Name DESC")

      Hope this works for you

      Comment

      • wbosw
        New Member
        • Mar 2007
        • 36

        #4
        Fellows,

        I was not able to get either one of the suggestions to work.

        Thanks anyway

        Wbosw

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          [code=vbnet]
          DataRow2 = dt.Select("Skil lsId = " & x & " AND SkillLevel > " & y & " AND
          MonthsExperienc e > " & w & " ", "Name DESC")
          [/code]

          If all of those columns are Int types then you do not want to use single-quotes

          Comment

          • wbosw
            New Member
            • Mar 2007
            • 36

            #6
            Plater, that works.

            Thank you

            Comment

            Working...