What I want is to check if a user entered value in textbox exists in database and if it does then go to page x, if not then page y type of thing. I've made a start but I've got some questions....
@{
var db = Database.Open("startersite");
var SQLSELECT = "SELECT refno FROM t_ticket where refno=@0";
if (IsPost){
var id = Request["formref"];
var oneref = db.QuerySingle(SQLSELECT,id);
var formref=oneref.refno;
var result = db.Execute (SQLSELECT, formref);
}
}
<form action="" method="post">
<input type="text" name = "formref">
<input type="submit">
</form>
So if I enter a value that does exist in database it doesn't throw up an exception, and if I enter a value that doesn't exist it throws up Cannot perform runtime binding on a null reference which I thought was a good base to build on as I know it's working.
However if I look at what value the 'result' variable has when a value does exist in database - it's always '-1' - that doesn't seem right?
@{
var db = Database.Open("startersite");
var SQLSELECT = "SELECT refno FROM t_ticket where refno=@0";
var id = "";
if (IsPost){
//make sure that the input is actually present
if(!Request["formref"].IsEmpty())
{
id = Request["formref"];
var result = db.QuerySingle(SQLSELECT,id);
if(result==null)
{
//do what you need to do when id is not found
}
else
{
//do what you need to do when id is found
}
}
else
{
ModelState.AddError("formref", "Error: you must enter a value");
}
}
}
@Html.ValidationSummary("Correct the following errors:")
<form action="" method="post">
<input type="text" name = "formref" value=@id>
<input type="submit">
</form>
Is it possible to set the 'action' attribute of a form dynamically (using webmatrix / c#?). I'm using the above method to redirect user to pageX or pageY depending on whether the reference number they entered exists in database, I'm using post method on
the form but the "action" will be pageX or pageY depending on whether the value they entered exists, so need a dynamic way of setting it. (I want to set the action so that I can use post method and have the value they entered already filled in on the page
they get redirected to)
I've tried a few methods using a variable called 'formaction' but can't seem to find a way of doing it - main thing is the form doesn't seem to accept variables for action attribute (?). I think you can do in javascript using something like document.form1.action
(not tried it yet) but just thought it would be nice to use razor code (as need to make call to server to check value of database so can just set this at same time using razor)
@GmGregori thanks I'd tried that but you did get me thinking and it now sort of works but not quite. The problem is even though the 'action' attribute gets set correctly it only gets set AFTER the form is submitted - so it would only work if the page was
submitted again (twice).
So currently what happens is:
1. default.cshtml page loads (form action = "")
2. User enters reference no in field and clicks submit (form action = user entered reference number)
(however user still on default.cshtml page because when they hit submit, the form action was empty)
(if they click submit again it will work but ideally I want it to be as streamlined as possible for user)
Maybe I do not understand what you want to do in this latest question.
Why do you want to use the action attribute of the form rather than the
method attribite?
It seems what you want to do is have the user enter something in the form and then hit the submit button, after which, you want the system to dynamically decide where the user should be redirected and redirect the user to that url.
If this is what you want, why not just use the "post" method on your form. Then in your code, have an if(IsPost) condition to make the decision to which url the user should go and then use Response.Redirect() to send the user to that url?
The following code should do what I describe here (warning - not tested - but you shoujld get the concept)
@{
var db = Database.Open("startersite");
var SQLSELECT = "SELECT refno FROM t_ticket where refno=@0";
var id = "";
if (IsPost)
{
//make sure that the input is actually present
if (!Request["formref"].IsEmpty())
{
id = Request["formref"];
var result = db.QuerySingle(SQLSELECT, id);
if (result == null)
{
//do what you need to do when id is not found
ModelState.AddError("formref", "Error: you must enter a value");
}
else
{
//do what you need to do when id is found
//testing the value that the user entered
switch (id)
{
case "value-x":
Response.Redirect(Href("~/url_for+handling_valuie_x?id=" + id));
break;
case "value-y":
Response.Redirect(Href("~/url_for+handling_valuie_y?id=" + id));
break;
//case ...
default:
ModelState.AddError("formref", "Reference ID is invalid - please try again");
break;
}
}
}
}
}
@Html.ValidationSummary("Correct the following errors:")
<form action="" method="post">
<input type="text" name = "formref" value=@id>
<input type="submit">
</form>
@rrrsr - thanks - I wanted the formref field on the redirected page to be already filled in - by using Request["formref"] from previous page? If there's a better way of doing it I'm very open to suggestions (I'm just starting out and doing this in spare
time - so just a novice to be honest :)
Thanks I'll experiment with your method above..... would your method use url parameters I'm guessing?
thinking about it you're right I could still request the ticketref from url (instead of the form value) on previous page - not sure why I didn't think of that
WK
Member
1 Points
11 Posts
Check a user entered value in database
Apr 12, 2012 04:19 PM|LINK
Hi All
What I want is to check if a user entered value in textbox exists in database and if it does then go to page x, if not then page y type of thing. I've made a start but I've got some questions....
@{ var db = Database.Open("startersite"); var SQLSELECT = "SELECT refno FROM t_ticket where refno=@0"; if (IsPost){ var id = Request["formref"]; var oneref = db.QuerySingle(SQLSELECT,id); var formref=oneref.refno; var result = db.Execute (SQLSELECT, formref); } } <form action="" method="post"> <input type="text" name = "formref"> <input type="submit"> </form>So if I enter a value that does exist in database it doesn't throw up an exception, and if I enter a value that doesn't exist it throws up Cannot perform runtime binding on a null reference which I thought was a good base to build on as I know it's working. However if I look at what value the 'result' variable has when a value does exist in database - it's always '-1' - that doesn't seem right?
Thanks in advance
query
stevenbey
All-Star
16526 Points
3378 Posts
Re: Check a user entered value in database
Apr 12, 2012 04:30 PM|LINK
Try this:
var oneref = db.QuerySingle(SQLSELECT,id); if(oneref != null) { var formref=oneref.refno; var result = db.Execute (SQLSELECT, formref); }query
http://stevenbey.com
Recursion: see Recursion
rrrsr7205
Participant
1304 Points
313 Posts
Re: Check a user entered value in database
Apr 12, 2012 05:27 PM|LINK
or this
@{ var db = Database.Open("startersite"); var SQLSELECT = "SELECT refno FROM t_ticket where refno=@0"; var id = ""; if (IsPost){ //make sure that the input is actually present if(!Request["formref"].IsEmpty()) { id = Request["formref"]; var result = db.QuerySingle(SQLSELECT,id); if(result==null) { //do what you need to do when id is not found } else { //do what you need to do when id is found } } else { ModelState.AddError("formref", "Error: you must enter a value"); } } } @Html.ValidationSummary("Correct the following errors:") <form action="" method="post"> <input type="text" name = "formref" value=@id> <input type="submit"> </form>WK
Member
1 Points
11 Posts
Re: Check a user entered value in database
Apr 12, 2012 08:49 PM|LINK
Thanks to both of you - rrrsr7205 I think your method works - I just had to remove the 'value' from the form field.
WK
Member
1 Points
11 Posts
Re: Check a user entered value in database
Apr 14, 2012 02:27 PM|LINK
Is it possible to set the 'action' attribute of a form dynamically (using webmatrix / c#?). I'm using the above method to redirect user to pageX or pageY depending on whether the reference number they entered exists in database, I'm using post method on the form but the "action" will be pageX or pageY depending on whether the value they entered exists, so need a dynamic way of setting it. (I want to set the action so that I can use post method and have the value they entered already filled in on the page they get redirected to)
I've tried a few methods using a variable called 'formaction' but can't seem to find a way of doing it - main thing is the form doesn't seem to accept variables for action attribute (?). I think you can do in javascript using something like document.form1.action (not tried it yet) but just thought it would be nice to use razor code (as need to make call to server to check value of database so can just set this at same time using razor)
Thanks in advance :)
GmGregori
Contributor
5470 Points
737 Posts
Re: Check a user entered value in database
Apr 14, 2012 02:57 PM|LINK
If you will set the 'action' attribute dynamically, you could you this razor syntax:
@{ var dest = "page.cshtml"; } <form action="@dest" method="post"> <input type="text" name = "formref" > <input type="submit"> </form>WK
Member
1 Points
11 Posts
Re: Check a user entered value in database
Apr 14, 2012 05:32 PM|LINK
@GmGregori thanks I'd tried that but you did get me thinking and it now sort of works but not quite. The problem is even though the 'action' attribute gets set correctly it only gets set AFTER the form is submitted - so it would only work if the page was submitted again (twice).
So currently what happens is:
1. default.cshtml page loads (form action = "")
2. User enters reference no in field and clicks submit (form action = user entered reference number)
(however user still on default.cshtml page because when they hit submit, the form action was empty)
(if they click submit again it will work but ideally I want it to be as streamlined as possible for user)
I'd appreciate any ideas
Thanks
rrrsr7205
Participant
1304 Points
313 Posts
Re: Check a user entered value in database
Apr 14, 2012 07:50 PM|LINK
WK:
Maybe I do not understand what you want to do in this latest question.
Why do you want to use the action attribute of the form rather than the method attribite?
It seems what you want to do is have the user enter something in the form and then hit the submit button, after which, you want the system to dynamically decide where the user should be redirected and redirect the user to that url.
If this is what you want, why not just use the "post" method on your form. Then in your code, have an if(IsPost) condition to make the decision to which url the user should go and then use Response.Redirect() to send the user to that url?
The following code should do what I describe here (warning - not tested - but you shoujld get the concept)
@{ var db = Database.Open("startersite"); var SQLSELECT = "SELECT refno FROM t_ticket where refno=@0"; var id = ""; if (IsPost) { //make sure that the input is actually present if (!Request["formref"].IsEmpty()) { id = Request["formref"]; var result = db.QuerySingle(SQLSELECT, id); if (result == null) { //do what you need to do when id is not found ModelState.AddError("formref", "Error: you must enter a value"); } else { //do what you need to do when id is found //testing the value that the user entered switch (id) { case "value-x": Response.Redirect(Href("~/url_for+handling_valuie_x?id=" + id)); break; case "value-y": Response.Redirect(Href("~/url_for+handling_valuie_y?id=" + id)); break; //case ... default: ModelState.AddError("formref", "Reference ID is invalid - please try again"); break; } } } } } @Html.ValidationSummary("Correct the following errors:") <form action="" method="post"> <input type="text" name = "formref" value=@id> <input type="submit"> </form>WK
Member
1 Points
11 Posts
Re: Check a user entered value in database
Apr 14, 2012 08:05 PM|LINK
@rrrsr - thanks - I wanted the formref field on the redirected page to be already filled in - by using Request["formref"] from previous page? If there's a better way of doing it I'm very open to suggestions (I'm just starting out and doing this in spare time - so just a novice to be honest :)
Thanks I'll experiment with your method above..... would your method use url parameters I'm guessing?
WK
Member
1 Points
11 Posts
Re: Check a user entered value in database
Apr 14, 2012 08:20 PM|LINK
thinking about it you're right I could still request the ticketref from url (instead of the form value) on previous page - not sure why I didn't think of that