Hi, can anyone tell me what I need to put after the ? in the action statement to pass the value entered in the textbox please. I have managed to get to the results page, but I can't display the text entered. I know how to retrieve the data on the new page
but I don't know what the correct format is for the action statement.
To handle the query in the receiving page use placeholders (starting with "@0") for the variable info,
try:
var s = Request.QueryString("field1"]; //retrieves the passed in value and puts it into variable s
var db=Database.Open("<databasename>"); //opens the database connection
var qry="SELECT * FROM tableName WHERE fieldname=@0"; // defines the SQL query with one parameter
var queryResult = db.Query(qry,s); // executes the query, passing the variabls "s" as the single parameter
I am not sure why you want to set the form on the sending page with a post method and an action.. I would just use post and in my code under the IfPost condition, do a Response.Redirect to the receiving page
Hi Thanks for the help but I am getting a compiler error:
Server Error in '/' Application.
Compilation Error
Description:
An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1955: Non-invocable member 'System.Web.HttpRequestBase.QueryString' cannot be used like a method.
Source Error:
Line 1: @{
Line 2: var s = Request.QueryString("field1"); Line 3: var db=Database.Open("myMusic");
Line 4: var qry="SELECT * FROM music WHERE fieldname=@0";
Source File: c:\Users\Ian\Documents\My Web Sites\myMusic\results.cshtml
Line: 2
Also, in your qry string, you need to use a real fild/column name in the "WHERE" clause. I used "fieldname" since I had no idea what your database design was.. So, let's assume you were selecting all rows in the "music" table where the
column whose name "title" has values equal to the parameter's value:
Ok I have changed some details but now getting this error message:
This is the new code in the results page:
@{
var s = Request.QueryString[field1];
var db=Database.Open("myMusic");
var qry="SELECT * FROM music WHERE Genre=@0"; // Genre now replaces fieldname
var queryResult = db.Query(qry,s);
}
Server Error in '/' Application.
Compilation Error
Description:
An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'field1' does not exist in the current context
Source Error:
Line 1: @{
Line 2: var s = Request.QueryString[field1]; Line 3: var db=Database.Open("myMusic");
Line 4: var qry="SELECT * FROM music WHERE Genre=@0";
Sorry just realised that field1 needed "" round it. But when doing that getting another error message: Not sure whats happening
Server Error in '/' Application.
Parameterized query expects a parameter value which was not supplied.
Parameter name: 0
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Parameterized query expects a parameter value which was not supplied.
Parameter name: 0
Source Error:
Line 3: var db=Database.Open("myMusic");
Line 4: var qry="SELECT * FROM music WHERE Genre=@0";
Line 5: var queryResult = db.Query(qry,s); Line 6: }
Line 7:
The system is telling you that it cannot find a passed-in variable named "field1" and, thus, has nothing to put into "s".
Are you getting to this page via the calling/sending page?
A way to debug this page is to comment out the line that is setting the "s" variable from the Request.QueryString. Then add a line that sets "s" to a value that you know is in the database's "genre" column: "var s = "<whatever that genre is>";
(You could also just type the url for this page along with "?field1=<whatever the genre is>" into the browser's address field and run it (as though the sending page had correctly done its job))
Then run the page and validate that, at least the query is working.
Once you have done that, remove the newly added "var s = ..." line and uncomment the line that sets it from the query string. Check out the code in your calling page. Try running the calling page and see if the variable gets into the receiving page.
If you have an error still, provide the calling page code.
Finally I Think I have cracked it. It was the way I was tryingf to display the variable at fault. For testing purposes I created a label and used the variable as a title. Data now passing nicely. Now all I have to do is get the database side working.....
(Gets up to brew 400th cup of coffee this afternoon...lol)
Thanks for the help, really appreciated that.
What I am trying to do is create a searchable music database. My code is probably not the most efficient way, but at time of writing I have been using Razor code and C# for about 1 week. Once I have cracked the little blocks that I need, I can put them together
and make something better.
I love this site, if you have a problem there is usually someone who knows the answer. Expect plenty more questions to come this way.
I, too, am kinda new at WebMatrix and Razor, though I have been around data and SQL Databases for a long time.
If my reply was helpful and answered your question to your satisfaction, please mark it as "answered" so the next guy comes along can find it.
You will find as you go along that a lot of the helpful references are, unfortunately, to ASP.Net Web Forms solutions and not WebPages (aka WebMatrix/Razor area) solutions. Those can lead the uninitiated down a lot of dead-end alleyways!
RRR
Marked as answer by Bramstone on Apr 08, 2012 04:02 PM
Bramstone
None
0 Points
12 Posts
Passing a textbox value through a hyperlink
Apr 08, 2012 10:05 AM|LINK
Hi, can anyone tell me what I need to put after the ? in the action statement to pass the value entered in the textbox please. I have managed to get to the results page, but I can't display the text entered. I know how to retrieve the data on the new page but I don't know what the correct format is for the action statement.
<form method="post" action="results.cshtml?field1=&text1.text">
<input type="text" name="text1" value=""/>
<input type="submit"/>
</form>
Also to save having to write another post, I need on the new page to create a custom sql search based on the textbox entry.
I know the constructor is "SELECT * FROM tablename WHERE columnName = (need the textbox entry here please)"
The code I am using for example purposes on the results page is:
@{
String s = Request.QueryString["field1"];
}
so any help as usual always appreciated.
Only been programming with asp.net for about a week, and you guys have already taught me loads, so many thanks for everything.
rrrsr7205
Participant
1304 Points
313 Posts
Re: Passing a textbox value through a hyperlink
Apr 08, 2012 01:36 PM|LINK
I assume you are using WebNatrix and Razor in your project
If yout text field on the sending page is named "text1" then you use that name as the variable following the "?":
results.cshtml?field1=@Request["text1"]
or "results?field1=" + Request["text1"]
To handle the query in the receiving page use placeholders (starting with "@0") for the variable info,
try:
var s = Request.QueryString("field1"]; //retrieves the passed in value and puts it into variable s
var db=Database.Open("<databasename>"); //opens the database connection
var qry="SELECT * FROM tableName WHERE fieldname=@0"; // defines the SQL query with one parameter
var queryResult = db.Query(qry,s); // executes the query, passing the variabls "s" as the single parameter
I am not sure why you want to set the form on the sending page with a post method and an action.. I would just use post and in my code under the IfPost condition, do a Response.Redirect to the receiving page
Bramstone
None
0 Points
12 Posts
Re: Passing a textbox value through a hyperlink
Apr 08, 2012 02:12 PM|LINK
Hi Thanks for the help but I am getting a compiler error:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1955: Non-invocable member 'System.Web.HttpRequestBase.QueryString' cannot be used like a method.
Source Error:
Line 1: @{ Line 2: var s = Request.QueryString("field1"); Line 3: var db=Database.Open("myMusic"); Line 4: var qry="SELECT * FROM music WHERE fieldname=@0";Source File: c:\Users\Ian\Documents\My Web Sites\myMusic\results.cshtml Line: 2
Not sure how to handle this?
rrrsr7205
Participant
1304 Points
313 Posts
Re: Passing a textbox value through a hyperlink
Apr 08, 2012 02:24 PM|LINK
Sorry - typo. The "()" should be "[]"
Try: Request.QueryString["field1"];
Also, in your qry string, you need to use a real fild/column name in the "WHERE" clause. I used "fieldname" since I had no idea what your database design was.. So, let's assume you were selecting all rows in the "music" table where the column whose name "title" has values equal to the parameter's value:
"SELECT * FROM music WHERE title - @0"
Bramstone
None
0 Points
12 Posts
Re: Passing a textbox value through a hyperlink
Apr 08, 2012 02:31 PM|LINK
Ok I have changed some details but now getting this error message:
This is the new code in the results page:
@{
var s = Request.QueryString[field1];
var db=Database.Open("myMusic");
var qry="SELECT * FROM music WHERE Genre=@0"; // Genre now replaces fieldname
var queryResult = db.Query(qry,s);
}
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'field1' does not exist in the current context
Source Error:
Line 1: @{ Line 2: var s = Request.QueryString[field1]; Line 3: var db=Database.Open("myMusic"); Line 4: var qry="SELECT * FROM music WHERE Genre=@0";
Bramstone
None
0 Points
12 Posts
Re: Passing a textbox value through a hyperlink
Apr 08, 2012 02:35 PM|LINK
Sorry just realised that field1 needed "" round it. But when doing that getting another error message: Not sure whats happening
Server Error in '/' Application.
Parameterized query expects a parameter value which was not supplied.
Parameter name: 0
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Parameterized query expects a parameter value which was not supplied.
Parameter name: 0
Source Error:
Line 3: var db=Database.Open("myMusic"); Line 4: var qry="SELECT * FROM music WHERE Genre=@0"; Line 5: var queryResult = db.Query(qry,s); Line 6: } Line 7:rrrsr7205
Participant
1304 Points
313 Posts
Re: Passing a textbox value through a hyperlink
Apr 08, 2012 02:47 PM|LINK
OK.
The system is telling you that it cannot find a passed-in variable named "field1" and, thus, has nothing to put into "s".
Are you getting to this page via the calling/sending page?
A way to debug this page is to comment out the line that is setting the "s" variable from the Request.QueryString. Then add a line that sets "s" to a value that you know is in the database's "genre" column: "var s = "<whatever that genre is>";
(You could also just type the url for this page along with "?field1=<whatever the genre is>" into the browser's address field and run it (as though the sending page had correctly done its job))
Then run the page and validate that, at least the query is working.
Once you have done that, remove the newly added "var s = ..." line and uncomment the line that sets it from the query string. Check out the code in your calling page. Try running the calling page and see if the variable gets into the receiving page.
If you have an error still, provide the calling page code.
Bramstone
None
0 Points
12 Posts
Re: Passing a textbox value through a hyperlink
Apr 08, 2012 03:12 PM|LINK
Finally I Think I have cracked it. It was the way I was tryingf to display the variable at fault. For testing purposes I created a label and used the variable as a title. Data now passing nicely. Now all I have to do is get the database side working..... (Gets up to brew 400th cup of coffee this afternoon...lol)
Thanks for the help, really appreciated that.
What I am trying to do is create a searchable music database. My code is probably not the most efficient way, but at time of writing I have been using Razor code and C# for about 1 week. Once I have cracked the little blocks that I need, I can put them together and make something better.
I love this site, if you have a problem there is usually someone who knows the answer. Expect plenty more questions to come this way.
Bramstone
rrrsr7205
Participant
1304 Points
313 Posts
Re: Passing a textbox value through a hyperlink
Apr 08, 2012 03:49 PM|LINK
I, too, am kinda new at WebMatrix and Razor, though I have been around data and SQL Databases for a long time.
If my reply was helpful and answered your question to your satisfaction, please mark it as "answered" so the next guy comes along can find it.
You will find as you go along that a lot of the helpful references are, unfortunately, to ASP.Net Web Forms solutions and not WebPages (aka WebMatrix/Razor area) solutions. Those can lead the uninitiated down a lot of dead-end alleyways!