Passing a textbox value through a hyperlinkhttp://forums.asp.net/t/1790405.aspx/1?Passing+a+textbox+value+through+a+hyperlinkSun, 08 Apr 2012 15:49:36 -040017904054921555http://forums.asp.net/p/1790405/4921555.aspx/1?Passing+a+textbox+value+through+a+hyperlinkPassing a textbox value through a hyperlink <p>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.&nbsp; </p> <p>&lt;form method=&quot;post&quot; action=&quot;results.cshtml?field1=&amp;text1.text&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input type=&quot;text&quot; name=&quot;text1&quot; value=&quot;&quot;/&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;input type=&quot;submit&quot;/&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/form&gt;</p> <p>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.</p> <p>I know the constructor is &quot;SELECT * FROM tablename WHERE columnName = (need the textbox entry here please)&quot;</p> <p>The code I am using for example purposes on the results page is:</p> <p>@{<br> &nbsp;&nbsp;&nbsp; String s = Request.QueryString[&quot;field1&quot;];<br> }</p> <p>so any help as usual always appreciated.</p> <p></p> <p>Only been programming with asp.net for about a week, and you guys have already taught me loads, so many thanks for everything.</p> 2012-04-08T10:05:53-04:004921691http://forums.asp.net/p/1790405/4921691.aspx/1?Re+Passing+a+textbox+value+through+a+hyperlinkRe: Passing a textbox value through a hyperlink <p>I<strong> assume you are using WebNatrix and Razor</strong> in your project</p> <p>If yout text field on the sending page is named &quot;text1&quot; then you use that name as the variable following the &quot;?&quot;:</p> <p><a href="mailto:results.cshtml?field1=@Request[&quot;text1">results.cshtml?field1=@Request[&quot;text1</a>&quot;]</p> <p>or &quot;results?field1=&quot; &#43; Request[&quot;text1&quot;]</p> <p>&nbsp;</p> <p>To handle the query in the receiving page use placeholders (starting with &quot;@0&quot;)&nbsp;for the variable info,</p> <p>try:</p> <p>var s = Request.QueryString(&quot;field1&quot;];&nbsp; //retrieves the passed in value and puts it into variable s<br> var db=Database.Open(&quot;&lt;databasename&gt;&quot;); //opens the database connection<br> var qry=&quot;SELECT * FROM tableName WHERE fieldname=@0&quot;; // defines the SQL query with one parameter<br> var queryResult = db.Query(qry,s); // executes the query, passing the variabls &quot;s&quot; as the single parameter</p> <p>&nbsp;I am not sure why you want to set the form on the sending page with a post method and an action..&nbsp; I would just use post and in my code under the IfPost condition, do a Response.Redirect to the receiving page</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2012-04-08T13:36:00-04:004921720http://forums.asp.net/p/1790405/4921720.aspx/1?Re+Passing+a+textbox+value+through+a+hyperlinkRe: Passing a textbox value through a hyperlink <p>Hi Thanks for the help but I am getting a compiler error:</p> <h1>Server Error in '/' Application.</h1> <hr size="1" width="100%" color="silver"> <h2><i>Compilation Error</i></h2> <p><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><b>Description: </b>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. <br> <br> <b>Compiler Error Message: </b>CS1955: Non-invocable member 'System.Web.HttpRequestBase.QueryString' cannot be used like a method.<br> <br> <b>Source Error:</b><br> <br> </span></span></p> <table width="100%" bgcolor="#ffffcc"> <tbody> <tr> <td></td> </tr> <tr> <td> <pre>Line 1: @{ <span color="red" style="color:red">Line 2: var s = Request.QueryString(&quot;field1&quot;); </span>Line 3: var db=Database.Open(&quot;myMusic&quot;); Line 4: var qry=&quot;SELECT * FROM music WHERE fieldname=@0&quot;; </pre> </td> </tr> </tbody> </table> <p><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><br> <b>Source File:</b> c:\Users\Ian\Documents\My Web Sites\myMusic\results.cshtml &nbsp;&nbsp; <b>Line:</b> 2 </span></p> <p></p> <p>Not sure how to handle this?</p> 2012-04-08T14:12:32-04:004921726http://forums.asp.net/p/1790405/4921726.aspx/1?Re+Passing+a+textbox+value+through+a+hyperlinkRe: Passing a textbox value through a hyperlink <p>Sorry - typo.&nbsp; The &quot;()&quot; should be &quot;[]&quot;</p> <p>Try: Request.QueryString<strong>[</strong>&quot;field1&quot;<strong>]</strong>;</p> <p>Also, in your qry string, you need to use a real fild/column name in the &quot;WHERE&quot; clause.&nbsp; I used &quot;fieldname&quot; since I had no idea what your database design was..&nbsp; So, let's assume you were selecting all rows in the &quot;<strong>music</strong>&quot; table where the column whose name &quot;<strong>title</strong>&quot; has values equal to the parameter's value:</p> <p>&quot;SELECT * FROM <strong>music</strong> WHERE <strong>title</strong> - @0&quot;</p> 2012-04-08T14:24:24-04:004921734http://forums.asp.net/p/1790405/4921734.aspx/1?Re+Passing+a+textbox+value+through+a+hyperlinkRe: Passing a textbox value through a hyperlink <p>Ok I have changed some details but now getting this error message:</p> <p>This is the new code in the results page:</p> <p>@{<br> &nbsp;&nbsp;&nbsp; var s = Request.QueryString[field1]; <br> &nbsp;&nbsp;&nbsp; var db=Database.Open(&quot;myMusic&quot;);<br> &nbsp;&nbsp;&nbsp; var qry=&quot;SELECT * FROM music WHERE Genre=@0&quot;; // Genre now replaces fieldname<br> &nbsp;&nbsp;&nbsp; var queryResult = db.Query(qry,s); <br> }</p> <h1>Server Error in '/' Application.</h1> <hr size="1" width="100%" color="silver"> <h2><i>Compilation Error</i></h2> <p><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><b>Description: </b>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. <br> <br> <b>Compiler Error Message: </b>CS0103: The name 'field1' does not exist in the current context<br> <br> <b>Source Error:</b><br> <br> </span></span></p> <table height="123" width="805" bgcolor="#ffffcc"> <tbody> <tr> <td></td> </tr> <tr> <td> <pre>Line 1: @{ <span color="red" style="color:red">Line 2: var s = Request.QueryString[field1]; </span>Line 3: var db=Database.Open(&quot;myMusic&quot;); Line 4: var qry=&quot;SELECT * FROM music WHERE Genre=@0&quot;; <br><br></pre> </td> </tr> </tbody> </table> 2012-04-08T14:31:53-04:004921737http://forums.asp.net/p/1790405/4921737.aspx/1?Re+Passing+a+textbox+value+through+a+hyperlinkRe: Passing a textbox value through a hyperlink <p>Sorry just realised that field1 needed &quot;&quot; round it. But when doing that getting another error message: Not sure whats happening</p> <h1>Server Error in '/' Application.</h1> <hr size="1" width="100%" color="silver"> <h2><i>Parameterized query expects a parameter value which was not supplied.<br> Parameter name: 0</i></h2> <p><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><b>Description: </b>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. <br> <br> <b>Exception Details: </b>System.ArgumentNullException: Parameterized query expects a parameter value which was not supplied.<br> Parameter name: 0<br> <br> <b>Source Error:</b> <br> <br> </span></span></p> <table width="100%" bgcolor="#ffffcc"> <tbody> <tr> <td> <pre>Line 3: var db=Database.Open(&quot;myMusic&quot;); Line 4: var qry=&quot;SELECT * FROM music WHERE Genre=@0&quot;; <span color="red" style="color:red">Line 5: var queryResult = db.Query(qry,s); </span>Line 6: } Line 7: </pre> </td> </tr> </tbody> </table> 2012-04-08T14:35:12-04:004921747http://forums.asp.net/p/1790405/4921747.aspx/1?Re+Passing+a+textbox+value+through+a+hyperlinkRe: Passing a textbox value through a hyperlink <p>OK.</p> <p>The system is telling you that it cannot find a passed-in variable named &quot;field1&quot; and, thus, has nothing to put into &quot;s&quot;.</p> <p>Are you getting to this page via the calling/sending page?</p> <p>A way to debug this page is to comment out the line that is setting the &quot;s&quot; variable from the Request.QueryString.&nbsp; Then add a line that sets &quot;s&quot; to a value that you know is in the database's &quot;genre&quot; column: &quot;var s = &quot;&lt;whatever that genre is&gt;&quot;;&nbsp;</p> <p>(You could also just type the url for this page along with &quot;?field1=&lt;whatever the genre is&gt;&quot; into the browser's address field and run it (as though the sending page had correctly done its job))</p> <p>Then run the page and validate that, at least the query is working.</p> <p>Once you have done that, remove the newly added &quot;var s = ...&quot; line and uncomment the line that sets it from the query string.&nbsp; Check out the code in your calling page.&nbsp; Try running the calling page and see if the variable gets into the receiving page.&nbsp;</p> <p>If you have an error still, provide the calling page code.</p> <p>&nbsp;</p> 2012-04-08T14:47:15-04:004921768http://forums.asp.net/p/1790405/4921768.aspx/1?Re+Passing+a+textbox+value+through+a+hyperlinkRe: Passing a textbox value through a hyperlink <p>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.....&nbsp;&nbsp; (Gets up to brew 400th cup of coffee this afternoon...lol)</p> <p>Thanks for the help, really appreciated that.</p> <p>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.</p> <p>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.</p> <p>Bramstone</p> 2012-04-08T15:12:18-04:004921792http://forums.asp.net/p/1790405/4921792.aspx/1?Re+Passing+a+textbox+value+through+a+hyperlinkRe: Passing a textbox value through a hyperlink <p>I, too, am kinda new at WebMatrix and Razor, though I have been around data and SQL Databases for a long time.</p> <p>If my reply was helpful and answered your question to your satisfaction, please mark it as &quot;answered&quot; so the next guy comes along can find it.</p> <p>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.&nbsp; Those can lead the uninitiated down a lot of dead-end alleyways!</p> 2012-04-08T15:49:36-04:00