C# - cannot get request.querystring to work!

t01621

Newcomer
Joined
Oct 3, 2008
Messages
5
Hi Everyone

I have been working with c# for only around 2 weeks so sorry if I don't explain the problem very well

I am trying to pass a value from one aspx page to another (topallowedsites.aspx is the posting page and topallowedsitesdetails.aspx is the receiving page)

In topallowedsites.aspx I have a gridview which pulls data from my database to show external websites that have been used by internal network users.
I want to pass the value of the 'domain' column to the topallowedsitesdetails.aspx page by clicking on a link in a HyperLinkField that is in the same row as the 'domain' value column
Please see below gridview code;
[highlight=asp]
<asp:gridview
id="GridView1"
showfooter="false"
runat="server"
width="99%"
autogeneratecolumns="False"
cellpadding="1" cellspacing="1"
font-family="verdana" font-size="11px"
EmptyDataText="<br /><h5><center>No record found! <br />Please try searching again.</center></h5>">

<columns>

<asp:BoundField datafield="Domain" headertext="Domain" sortexpression="Domain" DataFormatString="<a href=http://{0}>{0}</a>"> <ItemStyle Wrap="false"/></asp:boundfield>

<asp:BoundField datafield="Url" headertext="No of Urls" sortexpression="url"/>

<asp:BoundField datafield="Username" headertext="No of Users" sortexpression="username"/>

<asp:BoundField datafield="categoryname" headertext="Category" sortexpression="categoryname"/>

<asp:BoundField datafield="Bytes in" headertext="Bytes In" sortexpression="Bytes In"/>

<asp:BoundField datafield="Bytes out" headertext="Bytes Out" sortexpression="Bytes Out"/>

<asp:HyperLinkField DataNavigateUrlFields="domain" DataNavigateUrlFormatString="topallowedsitesdetails.aspx?domain={0}" Text="Url Details" HeaderText="Url Details" />

</columns>

</asp:gridview>
[/highlight]
If I click on the link in the hyperlinkfield it opens up the topallowedsitesdetails.aspx page and adds the correct domain information in the address bar i.e. if I clicked on the hyperlinkfield link beside a domain column value of google.com it redirects me to http://localhost/test/topallowedsitesdetails.aspx?domain=google.com

so far so good, however the topallowedsitesdetails.aspx page doesn't return any results in it's gridview? But If I use the asp:textbox search bar for a domain name such as google.com it does return results, so it's like the value is being passed but not added into the domain search variable

Below is the code I have in the <script> tags on topallowedsitesdetails.aspx to retrieve the extra details for google.com from the database;
C#:
protected void Page_Load(object sender, EventArgs e)
{
        string variable = Request.QueryString["domain"];
}
What am I missing? Hope someone can help

Many thanks
Tom
 
Last edited by a moderator:
Thanks for your reply PlausiblyDamp

The querystring is being passed correctly i.e. the correct domain is added to the end of the address after the '?'

The problem is when the details page loads it just loads everything from the table instead of using the domain in the query string to narrow down the results

The stored procedure is working correctly in sql so I think it must be that the details page isn't configured correctly to accept the domain variable

Like I said I'm new to using .net so could possibly be I haven't coded the details page correctly. Below is the code in the script tags on the details page the 'execute clicked' is there because I have some search textboxes on the page that search the gridview too

<script runat="server">

protected void Execute_Clicked(object sender, EventArgs e)
{
}

protected void Page_Load(object sender, EventArgs e)
{
string variable = Request.QueryString["domain"];

System.Data.SqlClient.SqlConnection objConn = new System.Data.SqlClient.SqlConnection();
objConn.ConnectionString = "server=.; database=ipmstatistics; integrated security=TRUE;";
objConn.Open();
System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand("usp_aspdetails", objConn);
objCmd.CommandType = System.Data.CommandType.StoredProcedure;
System.Data.SqlClient.SqlParameter tdomain = objCmd.Parameters.Add("@host", System.Data.SqlDbType.VarChar);
System.Data.SqlClient.SqlParameter tuser = objCmd.Parameters.Add("@user", System.Data.SqlDbType.VarChar);
System.Data.SqlClient.SqlParameter tcat = objCmd.Parameters.Add("@cat", System.Data.SqlDbType.VarChar);
System.Data.SqlClient.SqlParameter tpc = objCmd.Parameters.Add("@hostname", System.Data.SqlDbType.VarChar);

tdomain.Value = (hsearch.Text);
tuser.Value = (usearch.Text);
tcat.Value = (csearch.Text);
tpc.Value = (pcsearch.Text);

GridView1.DataSource = objCmd.ExecuteReader();
GridView1.DataBind();
objConn.Close();
}

</script>
 
From your code it looks as though you aren't doing anything with the variable once you have read it from the query string.

Does the stored proc have a parameter that will accept the domain? If not you might find it easier to use a datatable and a dataview rather than a datareader.
 
oh I see I thought I just had to use a request.querystring to accept the domain name

I already have a variable in sql to handle the domain. I am using the domain variable in an asp:textbox search, you can see the link to the @hostname sql variable which is used later in the code by a textbox to search for a domain

So I guess my question now is;
How can I get the request.querystring to pass the domain name into the @hostname sql variable to return the results from the database for that domain only, on page load that is

Many thanks again
 
oh and another quick question, is it bad practice to use a datareader or are they just harder to use, slower, etc
The returned data can be quite large sometimes
 
DataReaders are generally less functional that some of the other techniques but have very low overheads and therefore can be faster, especially with large result sets.
 
Thanks very much

I changed it to use tdomain instead of tpc.

tpc is for @hostname which is for a pc name search.

tdomain is for @host which is the domain name or website but then I got the following error;
'CS0103: The name 'tdomain' does not exist in the current context'

so instead of using tdomain.value I just used hsearch.Text

hsearch.Text = Request.QueryString["domain"];

Now I guess I had to use hsearch.Text because this is what the domain asp:textbox id is set to

This now puts the domain name I passed from the first page into the textbox search bar on the details page and then the 'protected void Page_Load' runs the search automatically when the page loads, which is exactly what I wanted so thank you very much!

It's all begining to make more sense now! :)

Do I have to mark this as the answer somewhere to give you credit like on other forums?
 
Back
Top