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>
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>