Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. get a variable from the query string

get a variable from the query string

Scheduled Pinned Locked Moved Web Development
databasecomquestiondiscussionlearning
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tamimi Code
    wrote on last edited by
    #1

    if i have a string that contains a URL, ana i need to extract a variable from the query string, i made two solutions , which one should i use ??

    Dim strUrl as String= "http://www.mySite.com?key=5&book=abc"
    dim strParam as String = "key"
    dim strResult as String
     
    Dim strKey As String = strUrl.ToLower.Substring(strUrl.IndexOf("?"))
    Dim strQuery As String() = strKey.Split("&")
    For i As Integer = 0 To strQuery.Length - 1
        If strQuery(i).ToLower.IndexOf(strParam.ToLower) <> -1 Then
            strQuery = strQuery(i).Split("=")
            strKey = strQuery(i)
            strResult = strKey
            Exit For
        End If
    Next i
     
    'or
     
    Dim strQuery As String = strURL.Substring(strURL.IndexOf("?") + 1)
    Dim objRequest As New HttpRequest("",strURL, strQuery)
    Dim strResult as String = objRequest.QueryString("key")
    

    your opinions please

    When you get mad...THINK twice that the only advice Tamimi - Code

    G 1 Reply Last reply
    0
    • T Tamimi Code

      if i have a string that contains a URL, ana i need to extract a variable from the query string, i made two solutions , which one should i use ??

      Dim strUrl as String= "http://www.mySite.com?key=5&book=abc"
      dim strParam as String = "key"
      dim strResult as String
       
      Dim strKey As String = strUrl.ToLower.Substring(strUrl.IndexOf("?"))
      Dim strQuery As String() = strKey.Split("&")
      For i As Integer = 0 To strQuery.Length - 1
          If strQuery(i).ToLower.IndexOf(strParam.ToLower) <> -1 Then
              strQuery = strQuery(i).Split("=")
              strKey = strQuery(i)
              strResult = strKey
              Exit For
          End If
      Next i
       
      'or
       
      Dim strQuery As String = strURL.Substring(strURL.IndexOf("?") + 1)
      Dim objRequest As New HttpRequest("",strURL, strQuery)
      Dim strResult as String = objRequest.QueryString("key")
      

      your opinions please

      When you get mad...THINK twice that the only advice Tamimi - Code

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Neither.

      Dim url as String= "http://www.mySite.com?key=5&book=abc"
      Dim key as String = "key"
      Dim result as String

      Dim queryString As String = url.Substring(url.IndexOf("?"))
      Dim params As String() = queryString.Split("&")
      For i As Integer = 0 To params.Length - 1
      Dim param As String() = params(i).Split("=")
      If param(0).ToLower = key.ToLower Then
      result = param(1)
      Exit For
      End If
      Next i

      --- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams

      T 1 Reply Last reply
      0
      • G Guffa

        Neither.

        Dim url as String= "http://www.mySite.com?key=5&book=abc"
        Dim key as String = "key"
        Dim result as String

        Dim queryString As String = url.Substring(url.IndexOf("?"))
        Dim params As String() = queryString.Split("&")
        For i As Integer = 0 To params.Length - 1
        Dim param As String() = params(i).Split("=")
        If param(0).ToLower = key.ToLower Then
        result = param(1)
        Exit For
        End If
        Next i

        --- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams

        T Offline
        T Offline
        Tamimi Code
        wrote on last edited by
        #3

        Guffa wrote:

        result = param(1)

        i think i made a type mistake , strKey = strQuery(1) :) could you please tell what's wrong with the second solution ?? the one that used the HttpRequest

        When you get mad...THINK twice that the only advice Tamimi - Code

        G 1 Reply Last reply
        0
        • T Tamimi Code

          Guffa wrote:

          result = param(1)

          i think i made a type mistake , strKey = strQuery(1) :) could you please tell what's wrong with the second solution ?? the one that used the HttpRequest

          When you get mad...THINK twice that the only advice Tamimi - Code

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Tamimi - Code wrote:

          i think i made a type mistake , strKey = strQuery(1)

          No, that's not a mistake. The array is named param, not strQuery. The result goes into the result variable, not some variable that isn't even declared.

          Tamimi - Code wrote:

          could you please tell what's wrong with the second solution ?? the one that used the HttpRequest

          The HttpRequest object is not supposed to be used that way. The documentation for the constructor says: "This constructor supports the .NET Framework infrastructure and is not intended to be used directly from your code."

          --- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams

          T 1 Reply Last reply
          0
          • G Guffa

            Tamimi - Code wrote:

            i think i made a type mistake , strKey = strQuery(1)

            No, that's not a mistake. The array is named param, not strQuery. The result goes into the result variable, not some variable that isn't even declared.

            Tamimi - Code wrote:

            could you please tell what's wrong with the second solution ?? the one that used the HttpRequest

            The HttpRequest object is not supposed to be used that way. The documentation for the constructor says: "This constructor supports the .NET Framework infrastructure and is not intended to be used directly from your code."

            --- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams

            T Offline
            T Offline
            Tamimi Code
            wrote on last edited by
            #5

            Guffa wrote:

            "This constructor supports the .NET Framework infrastructure and is not intended to be used directly from your code."

            i read that on the documentation, but i just wonding way, it could save the looping time. thank you for your response

            When you get mad...THINK twice that the only advice Tamimi - Code

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups