Results 1 to 8 of 8

Thread: Query string in windows app

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Query string in windows app

    My asp.net app will take query string as parameters like below:

    thhp://order.aspx?ordername=aaa&ordercity=london

    Can windows app do the same way? like below:

    order.exe aaa london

    order.exe will take "aaa" and "london" as passing parameter and process.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Query string in windows app

    Hello,

    You can pass ordername=aaa&ordercity=london to the executable then parse the string delimited by & then test to see what was given too you and if the needed information is there go forward, else decide what to do.

    Code:
    Dim arguments As String() = Environment.GetCommandLineArgs()
    Dim ordername As String = ""
    Dim ordercity As String = ""
    If arguments.Count = 2 Then
        Dim Temp = arguments(1).Split("&"c)
        For Each c In Temp
            If c.ToLower.Contains("ordername=") Then
                ordername = c.ToLower.Replace("ordername=", "")
            End If
            If c.ToLower.Contains("ordercity=") Then
                ordercity = c.ToLower.Replace("ordercity=", "")
            End If
        Next
    
        If Not String.IsNullOrWhiteSpace(ordername) AndAlso Not String.IsNullOrWhiteSpace(ordercity) Then
            Console.WriteLine("{0}  {1}", ordername, ordercity)
        End If
    End If
    To test this, under project properties, Debug tab enter under Start Options -> Command line arguments ordername=aaa&ordercity=london then in form load use the code above.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Re: Query string in windows app

    I tried but got two errors said that
    arguments.Count is not a member of "system.array"
    'c' is not declared.
    Last edited by aspfun; Apr 30th, 2014 at 10:41 AM.

  4. #4
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Query string in windows app

    For one, the count of the comandlineargs will always be at least one (it includes the exe name), you should check vs 3
    if you are getting c is not declared is because it is not, you must have Option Explicit on (as you should) then either declare c on the dim section or on the loop itself (For Each c As string).
    the other error we should see how you typed the code. Because it looks like you did something like array.arguments.count
    Last edited by kaliman79912; Apr 30th, 2014 at 11:36 AM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  5. #5
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Query string in windows app

    You can pass the arguments as you posted, without the placeholder, just the values separated by spaces

    Code:
            Dim arguments As String() = Environment.GetCommandLineArgs()
            Dim ordername As String = ""
            Dim ordercity As String = ""
    
            If arguments.Count = 3 Then
                ordername = arguments(1)
                ordercity = arguments(2)
            End If
    This is kevin's code adapted to what I just said. I am not saying this is the correct way, its less explicit and prone to user mistakes, but it is simpler.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  6. #6
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Query string in windows app

    I just noticed that kevin was checking vs 2 not because of your two arguments, but because he is handling both in just one single string. My bad.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Query string in windows app

    Quote Originally Posted by aspfun View Post
    I tried but got two errors said that
    arguments.Count is not a member of "system.array"
    'c' is not declared.
    Revised
    Code:
    Dim arguments As String() = Environment.GetCommandLineArgs()
    Dim ordername As String = ""
    Dim ordercity As String = ""
    If arguments.Count = 2 Then
        Dim Temp = arguments(1).Split("&"c)
        For Each c As String In Temp
            If c.ToLower.Contains("ordername=") Then
                ordername = c.ToLower.Replace("ordername=", "")
            End If
            If c.ToLower.Contains("ordercity=") Then
                ordercity = c.ToLower.Replace("ordercity=", "")
            End If
        Next
        If Not String.IsNullOrWhiteSpace(ordername) AndAlso Not String.IsNullOrWhiteSpace(ordercity) Then
            Console.WriteLine("{0}  {1}", ordername, ordercity)
        End If
    End If
    I do a good deal of coding which requires Option Infer=On for LINQ/Lambda so I did not cast c as string. I always code with Option Strict On, Option Explicit On, Option Infer On. Now with that mentioned, I do not recommend you or anyone else using Option Infer On unless you have a reason, otherwise it can cause more problems then it's worth. Once you work with LINQ/Lamba which create Anonymous Types you will understand. So in my projects I have the options as just mentioned while some I will have Option Infer Off and Option Strict On in specific classes.

    Example which requires Option Infer On
    Code:
    Dim Results =
        (
            From T In TemperaturesDict.GroupBy(
            Function(f) 3 > 0)) _
            .Select(Function(group) _
                New With
                {
                    .High = group.Max(Function(y) y.Value),
                    .Low = group.Min(Function(y) y.Value),
                    .Avgerage = group.Average(Function(y) y.Value)
                }
    ).First
    
    Console.WriteLine("Average: {0} High: {1} Low: {2}",
                      Results.Avgerage,
                      Results.High,
                      Results.Low
    )

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Query string in windows app

    Quote Originally Posted by aspfun View Post
    My asp.net app will take query string as parameters like below:

    thhp://order.aspx?ordername=aaa&ordercity=london

    Can windows app do the same way? like below:

    order.exe aaa london

    order.exe will take "aaa" and "london" as passing parameter and process.
    Yes, this has existed since long before the internet, it's called "Command Line Arguments", I suggest reading up on it & of course you're not the first to ask about it in the .Net age: http://www.vbdotnetforums.com/consol...arameters.html
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width