Click here to Skip to main content
15,888,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a two text boxes and i want to take amount and person from user and divide amount/person and convert result into double
here is my code but it will give me error, Here is my code and below is the erro code
C#
protected void btnSearch_Click(object sender, EventArgs e)
{
if((txtAmount.Text == "") || (txtPerson.Text == ""))
{
Response.Write("please enter amount");
} 
else
{
double amount = int.Parse(txtAmount.Text);
double persons = int.Parse(txtPerson.Text);
double result = amount / persons;
d.Price = Convert.ToInt32(result);
d.Select(d);
Display();
}


C#
Line 39:             else
Line 40:             {
Line 41:                 double amount = int.Parse(txtAmount.Text);
Line 42:                 double persons = int.Parse(txtPerson.Text);
Line 43:                 double result = amount / persons;
Posted
Updated 16-Feb-14 20:46pm
v2

Repalce
C#
double amount = int.Parse(txtAmount.Text);
double persons = int.Parse(txtPerson.Text);

With
C#
double amount = Double.Parse(txtAmount.Text);
double persons = Double.Parse(txtPerson.Text);

This may help.
 
Share this answer
 
v4
Comments
Ali Ashiq 17-Feb-14 2:58am    
still not working it will give error on this line
double amount = int.TryParse(txtAmount.Text);
Gitanjali Singh 17-Feb-14 3:01am    
check updated solution.
Ali Ashiq 17-Feb-14 3:06am    
double amount = Double.TryParse(txtAmount.Text);
still giving error :(
Gitanjali Singh 17-Feb-14 3:14am    
Debug and check the value of textbox txtAmount.What error you are getting?
Gitanjali Singh 17-Feb-14 3:22am    
Please Check updated solution.This is working fine on my end.
I think your code does not validate input values properly.

C#
if((txtAmount.Text == "") || (txtPerson.Text == ""))
{
    Response.Write("please enter amount");
}


is only for "" or String.Empty. What about " " (one space)?

And what about invalid values (NaN - not a number values)? You should modify this code to validate them and give proper error messages.

txtAmount.Text.Trim() is a good call (for the other text box as well) to cut down spaces.

Also, use TryParse() to validate against NaN and non-double values. It returns false if the validation fails.

Ex :-

C#
double amount;

if (double.TryParse(txtAmount.Text, out amount))
 
Share this answer
 
Comments
Ali Ashiq 17-Feb-14 4:06am    
sir i didn't get your point will you please me correct my code if u did i shall be very thankful to you for kind act
Ali Ashiq 17-Feb-14 4:06am    
Exception Details: System.FormatException: Input string was not in a correct format.
this is the exception
OK. Please try this.

C#
string amountText = txtAmount.Text;
string personsText = txtPerson.Text;

double amount;
double persons;

if (double.TryParse(amountText.Trim(), out amount) && double.TryParse(personsText.Trim(), out persons))
{
    //both amountText and personsText are valid.
    double result = amount / persons;
}
else
{
    //something is invalid. show some error.
}
 
Share this answer
 
Comments
Ali Ashiq 17-Feb-14 4:47am    
All working perfectly sir but a minor problem is occured is that error message is begin/show on the top of the page, didn't show message near and under that text box
Ali Ashiq 17-Feb-14 4:48am    
Thank you so much sir i shall be very thankful to you for that kind act...
wmdroshan 17-Feb-14 4:53am    
Please kind enough to mark right solutions as answered. Thanks.
Ali Ashiq 17-Feb-14 4:57am    
ok i will going to mark as answered please tell me how could error message is set under the textbox
wmdroshan 17-Feb-14 5:03am    
What do you mean by "set under the textbox" ? Simply show it under the text box? This depends on the technology you use (WinForms, WPF, etc...) Normally there are Validators. Check this out as a start point, http://msdn.microsoft.com/en-us/library/ms229603%28v=vs.110%29.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900