Hello everyone,
Could someone please give me a good explanation about what I can do with it? I can't really find it on Google..
Thanks in advance,
Davey

Recommended Answers

All 17 Replies

I use querystring to retrieve parameters send from another form, let's say that you are and index.html and there is a link url to default.aspx like this
~/Default.aspx?var=hello

then and the Server code from Default.aspx to retrieve that value i have this.

string stTest = Request.QueryString["var"];

now stTest is equal to "hello"
hope that help you

commented: good explanation +9
commented: if sknake thinks it is worth it, it is worth it +7

Its handy if you want to pass data to other pages, especially when you want it retained in the browser's history. If you POST search terms to google for example then you wouldn't have a list of all searches you performed in your browser history.

commented: repute back function +7

It comes from the days of ASP.

The "Request.QueryString" command is used to collect values in a form with method="get".

SimpleForm_1.asp

<form method="get" action="SimpleFormHandler_1.asp">
    <TEXTAREA NAME="location" COLS=33 ROWS=4></TEXTAREA>
    <input type='submit' value='Add Event'>
</form>

SimpleFormHandler_1.asp

<%@ Language=VBScript %>

<%
        Dim userLocation

        'location is the variable name as defined by "NAME="
        'in SimpleForm_1.asp
        userLocation = Request.QueryString("location")
%>

The "Request.Form" command is used to collect values in a form with method="post".

SimpleForm_2.asp

<form method="post" action="SimpleFormHandler_2.asp">
    <TEXTAREA NAME="location" COLS=33 ROWS=4></TEXTAREA>
    <input type='submit' value='Add Event'>
</form>

SimpleFormHandler_2.asp

<%@ Language=VBScript %>

<%
        Dim userLocation

        'location is the variable name as defined by "NAME="
        'in SimpleForm_2.asp
        userLocation = Request.form("location")
%>

See http://www.w3schools.com/ASP/asp_inputforms.asp

You'll probably find more info on it if you search for "ASP Request.QueryString" and "ASP Request.Form".

C#

string userLocation;
userLocation = Request.QueryString["location"];

C#

string userLocation;
userLocation = Request.Form["location"];

this thread must be moved to ASP.NET forum.

Ok, I will flag this thread then.
Ok, so I read all of your comments, and thanks for it :)
I'm gonna ask another question about this.

I need to get the id=123 in http://www.website.com/something.php?id=123, in my C# application. Whenever I press a new button on the website the C# app needs to get the new id, or
value etc etc. So would this then be the best solution to get it? Or could I better use something else..?

Thanks again,
Davey

And, I can't find the flag button :(

I flagged it for you. The button should be located under the reply:

sknake Online
Posting Virtuoso
Re: QueryString #9 1 Minute Ago | Add to sknake's Reputation | Flag Bad Post

Oh yes, thanks haha.
How could I miss that? :P

Ok, so I got a bit of a solution at this point, I got the PHP page sending the QueryString to another PHP page, which sends it to my C# app, at least.. its suppose to do that. At this point I get the entire code of my page! Not good, anyone got an idea about what's wrong? Here is my code:

PHP to get the QueryString (There is more, but this is the only bit that matters)

<?
require('sendtoC.php');
$send = new sendtoC();

if($_GET['input'] == "")
{
    echo "Variabele is leeg.";
}
else
{
	$var = $_GET['input'];
    $send->write($var);
}
?>

PHP code to receive the QueryString, and send it to C# app

<?
class sendtoC {
function write($var) {
	print($var); 
	}
}
?>

the C# app

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.IO;

namespace MVC_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string ConnectWeb()
        {
            string website = @"http://www.daveybrouwer.eu/test.php";
            WebRequest request = WebRequest.Create(website);
            WebResponse response = request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string query = reader.ReadLine();
            return query;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = ConnectWeb();
        }
    }
}

Your help is greatly appreciated,
Davey

Do you not want to get the entire page?

namespace daniweb.web
{
  public partial class Cb : System.Web.UI.Page
  {
    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      if (Request.QueryString["parm"] != null)
      {
        Response.Write("You entered the value: " + Request.QueryString["parm"]);
        Response.Flush();
        Response.Close();
      }
    }

URL:
http://localhost:14332/Cb.aspx?parm=hello

Result:

You entered the value: hello

Do you not want to get the entire page?

namespace daniweb.web
{
  public partial class Cb : System.Web.UI.Page
  {
    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      if (Request.QueryString["parm"] != null)
      {
        Response.Write("You entered the value: " + Request.QueryString["parm"]);
        Response.Flush();
        Response.Close();
      }
    }

URL:
http://localhost:14332/Cb.aspx?parm=hello

Result:

You entered the value: hello

Could you give me some explanation about this? I dont really get it.. sorry :(

Explain what you are trying to do first.

>>. At this point I get the entire code of my page! Not good, anyone got an idea about what's wrong? Here is my code:

From what you said I was guessing that when you entered a query string you didn't want to render the page, but rather wanted to only render something custom. Please clarify what you are trying to do

Ok,

well when the index page is loaded and I press on a button on that page, I want to send the link of that button (well the QueryString) to my C# application. So that my C# can see what the QueryString is, and then save something to a database, or render a new page etc. etc. I am trying to make it along the MVC pattern, this is how I understood MVC works.. If something isn't clear please ask.

Regards again,
Davey :)

MVC uses URL rewriting to mask the query string. Daniweb uses URL rewriting too. By default vBulleting's urls look like

http://www.daniweb.com/forums/viewThread.php?id=214743&page=2

Notice how Dani made them look. This helps search engines index your pages better:

http://www.daniweb.com/forums/thread214743-2.html

Anyway you don't need to send the URL with your button. The button causes a post back and you can get the current URL with Request.Url

I can't get this to work in my C# application or in my PHP page.. What am I doing wrong?

I have no idea what you're doing -- so I don't know what you're doing wrong.

Explain what you are trying to do first.

>>. At this point I get the entire code of my page! Not good, anyone got an idea about what's wrong? Here is my code:

From what you said I was guessing that when you entered a query string you didn't want to render the page, but rather wanted to only render something custom. Please clarify what you are trying to do

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.