Click here to Skip to main content
15,791,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good day everyone please i am working on an application. i want the user to be able to enter the amount of loan they want to borrow, and if the loan is within 1 year it should work. so depending on the months the user is going to borrow the loan. for 6months or lesser the program should multiply the amount entered by the user by 5% within six - nine months 7.5% of the money the user enter so if the datatimepicker(days) difference is lesser than 6months(before) but i am getting error

What I have tried:

int result; 
DateTime reqdt = dtrequest.Value.Date;
DateTime retdt = dtreturn.Value.Date;

DateTime before = reqdt.AddMonths(6);
DateTime within = reqdt.AddMonths(9);
DateTime after = reqdt.AddYears(1);
TimeSpan ts = retdt - reqdt;

int days = ts.Days;
DateTime cde = new DateTime(days);// days to time 
// DateTime cde = Convert.ToDateTime(days);
result = Convert.ToInt32(txtamount.Text);//....................converting string to int(error not in the correct format)

if (cde <= before)
   double mult = result * 5;
   txtresult.Text = mult.ToString();
Updated 25-May-19 22:47pm
Patrice T 25-May-19 23:26pm    
What is the contain of txtamount ?
akinwunmi 26-May-19 2:56am    
i want to the user to enter the amount of money they want to borrow(txtamount i.e textbox for amount)
phil.o 26-May-19 4:22am    
Patrice's question was: what is the actual content of the text box when the error happens? To what value does txtamount.Text equal?
akinwunmi 26-May-19 14:27pm    
result = Convert.ToInt32(txtamount.Text)
phil.o 26-May-19 14:33pm    
Sorry, this is still not the question.

1 solution

Use int.TryParse[^] instead. You get a true/false value back from the method call telling you whether the parsing operation succeeded or not.

Now, you cannot convert an integer to a DateTime. It doesn't make any sense to do so.

You CAN, however, add hours, minutes, seconds, days, months, ...whatever to an existing date/time value but that has to have a starting date/time to work with, like you're already doing near the top of your code snippet.

In your code snippet, you can use the ts.Days value in a call to .AddDays() on some existing DateTime value.
Share this answer

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