How to select data from database using a dropdownlist for this code block, instead of
lastRecord.USD let the users select also lastRecord.EUR,
lastRecord.GBP and lastRecord.CHF
@{
var db = Database.Open("money");
var lastRecord = db.QuerySingle("SELECT TOP 1 * FROM foreign ORDER BY ID DESC");
var Result = 0m;
if(IsPost) {
var Price = decimal.Parse(lastRecord.USD);
var UserInput = Request["UserInput"].AsDecimal();
if (UserInput == 0) {
ModelState.AddError("UserInput", "Enter amount!");
}
// calculate
Result = UserInput * Price;
}
}
<form action="" method="post">
<table>
<tr>
<td> // use here a select dropdownlist //</td>
<td><input type="text" name="UserInput" size="14" value="1"/></td>
</tr>
</table>
</form>
<text>Result: @Result</text>
two more questions, how to avoid the error message if the user leave the option 0 (<option value="0">Select</option>)? Instead use something like this code, but for the list because i get error:Invalid column name :
var UserInput = Request["UserInput"].AsDecimal();
if (UserInput == 0) {
ModelState.AddError("UserInput", "Enter amount!");
}
and how to display the Result after the submit, (for the code below)
@{
var db = Database.Open("money");
var lastRecord = db.QuerySingle("SELECT TOP 1 * FROM foreign ORDER BY ID DESC");
var Result = 0m;
var LastValue = Request["UserInput"];
var LastPrice = Request["currency"];
if(IsPost) {
var Price = decimal.Parse(lastRecord[Request["currency"]]);
var UserInput = Request["UserInput"].AsDecimal();
if (UserInput == 0) {
ModelState.AddError("UserInput", "Enter amount!");
}
// calculate
Result = UserInput * Price;
}
}
<form action="" method="post">
<table>
<tr>
<td><select name="currency">
<option>Select</option>
<option>USD</option>
<option>EUR</option>
<option>GBP</option>
<option>CHF</option>
<option>JPY</option>
</select></td>
</tr>
<tr>
<td><input type="text" name="UserInput" size="14" value="1"/> <input type="submit" value="Go"/></td>
</tr>
</table>
</form>
<text>@if(ModelState.IsValid) {<span>Result:</span> @LastValue<span> </span>@LastPrice @Result} else {@Html.ValidationSummary()}</text>
how to avoid the error message if the user leave the option 0 (<option value="0">Select</option>)? Instead use something like this code, but for the list because i get error:Invalid column name
The option 0 (<option value="0">Select</option>) it's needed in my example using jQuery, because the result is displayed when the user makes a selection in the dropdown list.
Otherwise it's not needed: the first row (USD?) is selected by itself and the user can accept or change it before to click the "Go" button.
For the second question, the last row of your code formats the result layout
@{
var db = Database.Open("money");
var lastRecord = db.QuerySingle("SELECT TOP 1 * FROM foreign ORDER BY ID DESC");
var Result = 0m;
var Price = 0m;
var LastValue = Request["UserInput"];
var LastPrice = Request["currency"];
if(IsPost) {
if (LastPrice != "Select") {
Price = decimal.Parse(lastRecord[LastPrice]);
} else {
ModelState.AddError("Price", "Select currency!");
}
var UserInput = Request["UserInput"].AsDecimal();
if (UserInput == 0) {
ModelState.AddError("UserInput", "Enter amount!");
}
// calculate
if (ModelState.IsValid) {
Result = UserInput * Price;
}
}
}
dow7
Member
738 Points
452 Posts
How to select data from database using a dropdownlist
Feb 19, 2012 12:10 PM|LINK
How to select data from database using a dropdownlist for this code block, instead of lastRecord.USD let the users select also lastRecord.EUR, lastRecord.GBP and lastRecord.CHF
@{ var db = Database.Open("money"); var lastRecord = db.QuerySingle("SELECT TOP 1 * FROM foreign ORDER BY ID DESC"); var Result = 0m; if(IsPost) { var Price = decimal.Parse(lastRecord.USD); var UserInput = Request["UserInput"].AsDecimal(); if (UserInput == 0) { ModelState.AddError("UserInput", "Enter amount!"); } // calculate Result = UserInput * Price; } } <form action="" method="post"> <table> <tr> <td> // use here a select dropdownlist //</td> <td><input type="text" name="UserInput" size="14" value="1"/></td> </tr> </table> </form> <text>Result: @Result</text>GmGregori
Contributor
5574 Points
749 Posts
Re: How to select data from database using a dropdownlist
Feb 19, 2012 01:41 PM|LINK
You can refer to a lastRecord column with this syntax:
so you can use a dropdown list as this one:
<select name="currency"> <option>USD</option> <option>EUR</option> <option>GBP</option> <option>CHF</option> </select>and replace the row
with
dow7
Member
738 Points
452 Posts
Re: How to select data from database using a dropdownlist
Feb 19, 2012 02:46 PM|LINK
Thank you,
is there a way to keep the selected option after the submit?
GmGregori
Contributor
5574 Points
749 Posts
Re: How to select data from database using a dropdownlist
Feb 19, 2012 10:18 PM|LINK
Maybe you could do all the work in client side with jQuery in a page like this one:
@{ var db = Database.Open("money"); var lastRecord = db.QuerySingle("SELECT TOP 1 * FROM foreign ORDER BY ID DESC"); } <head> <script src="@Href("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script> </head> <div> <select name="currency" id="currency"> <option value="0">Select</option> <option value="@lastRecord.USD">USD</option> <option value="@lastRecord.EUR">EUR</option> <option value="@lastRecord.GBP">GBP</option> <option value="@lastRecord.CHF">CHF</option> </select> <input type="text" id="UserInput" size="14" value="1"/> <input type="text" id="Result" size="14" value="0"/> </div> <script> $('#currency').change(performVal); function performVal() { var amount = parseFloat($('#UserInput').val()); var rate = parseFloat($("#currency").val()); var result = amount * rate; $('#Result').val(result); } </script>Pay attention to the jQuery library link.
dow7
Member
738 Points
452 Posts
Re: How to select data from database using a dropdownlist
Feb 21, 2012 03:48 PM|LINK
Thank you a lot,
two more questions, how to avoid the error message if the user leave the option 0 (<option value="0">Select</option>)? Instead use something like this code, but for the list because i get error:Invalid column name :
var UserInput = Request["UserInput"].AsDecimal();
if (UserInput == 0) {
ModelState.AddError("UserInput", "Enter amount!");
}
and how to display the Result after the submit, (for the code below)
@{ var db = Database.Open("money"); var lastRecord = db.QuerySingle("SELECT TOP 1 * FROM foreign ORDER BY ID DESC"); var Result = 0m; var LastValue = Request["UserInput"]; var LastPrice = Request["currency"]; if(IsPost) { var Price = decimal.Parse(lastRecord[Request["currency"]]); var UserInput = Request["UserInput"].AsDecimal(); if (UserInput == 0) { ModelState.AddError("UserInput", "Enter amount!"); } // calculate Result = UserInput * Price; } } <form action="" method="post"> <table> <tr> <td><select name="currency"> <option>Select</option> <option>USD</option> <option>EUR</option> <option>GBP</option> <option>CHF</option> <option>JPY</option> </select></td> </tr> <tr> <td><input type="text" name="UserInput" size="14" value="1"/> <input type="submit" value="Go"/></td> </tr> </table> </form> <text>@if(ModelState.IsValid) {<span>Result:</span> @LastValue<span> </span>@LastPrice @Result} else {@Html.ValidationSummary()}</text>GmGregori
Contributor
5574 Points
749 Posts
Re: How to select data from database using a dropdownlist
Feb 22, 2012 12:48 PM|LINK
I'm not sure to understand what you mean.
The option 0 (<option value="0">Select</option>) it's needed in my example using jQuery, because the result is displayed when the user makes a selection in the dropdown list.
Otherwise it's not needed: the first row (USD?) is selected by itself and the user can accept or change it before to click the "Go" button.
For the second question, the last row of your code formats the result layout
and you get something like: Result: 1 USD0.045
In your code there are some unnecessary span tags, but, apart from that, you can format the output as you prefer.
dow7
Member
738 Points
452 Posts
Re: How to select data from database using a dropdownlist
Feb 23, 2012 03:54 PM|LINK
i mean for the code above (http://forums.asp.net/post/4843467.aspx) without javascript, i leave the option 0 (<option value="0">Select</option>).
So, i want to add a ModelState.AddError for this option, if the user leave option 0.
Its something like this example but is not as AsInt, is as AsDecmial
if (Request["employees"].IsInt()) { employeecount = Request["employees"].AsInt(); } else { errors = true; @:Employee count must be a number.<br /> }GmGregori
Contributor
5574 Points
749 Posts
Re: How to select data from database using a dropdownlist
Feb 23, 2012 07:35 PM|LINK
Try the code that follows:
@{ var db = Database.Open("money"); var lastRecord = db.QuerySingle("SELECT TOP 1 * FROM foreign ORDER BY ID DESC"); var Result = 0m; var Price = 0m; var LastValue = Request["UserInput"]; var LastPrice = Request["currency"]; if(IsPost) { if (LastPrice != "Select") { Price = decimal.Parse(lastRecord[LastPrice]); } else { ModelState.AddError("Price", "Select currency!"); } var UserInput = Request["UserInput"].AsDecimal(); if (UserInput == 0) { ModelState.AddError("UserInput", "Enter amount!"); } // calculate if (ModelState.IsValid) { Result = UserInput * Price; } } }