Can't get querystring to post to second page

C

Chumley Walrus

I've got a c# script whereas I am trying to post from a textbox on my
firstpage.aspx over to page2.aspx, but the error I get is:The name
'txtstuff' does not exist in the class or namespace 'ASP.page2_aspx'


firstpage.aspx button object subroutine:
///////////////////////
private void btnSubmit_Click(object sender, System.EventArgs e)
{
Response.Redirect("dg4.aspx?txtboarddate=" +
this.txtboarddate.Text);
}
///////////////////////


page2.aspx script:
//////////////////////////////////////////


<%@ Import Namespace="System.Data"%>

<%@ Import Namespace="System.Data.SqlClient"%>



<script Language="C#" runat="server">



private void Page_Load(object sender, System.EventArgs e)

{

txtstuff = Request.QueryString["txtstuff"];



if(txtstuff != null){



if(txtstuff =="") txtstuff = "Visitor";

}

////txtstuff = Request.QueryString["txtstuff "];

////this.txtstuff .Text = Request.QueryString["txtstuff "];



BindMe();

}





// function to bind the records retrieved

// from the database to "dgrid" grid

void BindMe() {

////

string txtstuff = Request.QueryString["txtstuff"];

// build the connection string

string strConn = "SERVER=xxx.xx.xx.xx;UID=xxx;PWD=xxxx;DATABASE=mydb;";




// connect to the database

SqlConnection objConn = new SqlConnection(strConn);


// query

string strSQL = "SELECT * FROM tblDb where MyDate = '" & txtstuff &
"'";



// create an instance of the DataReader object

SqlCommand objCommand = new SqlCommand(strSQL, objConn);

objConn.Open();

SqlDataReader objReader = objCommand.ExecuteReader();


// assign the DataReader object as the source

// for the "dgrid" grid

dgrid.DataSource = objReader;

dgrid.DataBind();

// free up memory

objReader.Close();

objConn.Close();

}

</script>


???????????????
chumley
 
M

Marina

You never declared the variable 'txtstuff'. You just tried to assign it a
value.
 
C

Chumley Walrus

No sorry, the querystring name is 'txtstuff', not txtboarddate, as
txtstuff is just a generic example

I thought I was declaring it (???)
 
M

Marina

You are not declaring the variable. You have 2 things named 'txtstuff'. One
is something you are passing in query string. The other is a variable you
are trying to use on the second page.

Notice that this is a compile time error. Not a runtime error. The compiler
cannot compile your page because you are trying to use a variable you have
not declared. This would be a reason to use code-behind and not put scripts
right in the page - much easier to work out compile time errors in the IDE.
 
C

Chumley Walrus

I tried declaring as such in the code below, but now I get an error in
my sql statement : Operator '&' cannot be applied to operands of type
'string' and 'string'
////////////////
string rqs = Request.QueryString["txtmystuff"];
string strConn = "SERVER=xx.xx.xx.xx;UID=xx;PWD=xxx;DATABASE=Mydb;";


// connect to the database
SqlConnection objConn = new SqlConnection(strConn);

// query
string strSQL = "SELECT * FROM tblDb where OrderDate = '" & rqs & "'";
 
M

Marina

& is a concatenation operator in VB. Not in C#. You might want to read up
some on C#.
 
C

Chumley Walrus

string strSQL = "SELECT * FROM tblDb where OrderDate = '" + rqs + "'";


....is the correct syntax . It all works now

< read up some on C#

But why?, if I can sit on your lap with a big glass of milk and we can
go over it in the book together :eek:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top