Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
want to add months to start date and display in end date .my code is

i have 3 text boxes

Number of months
Start date
End date

when i enter 5 months those add to start date and display that in end date .when start date text box index changed

i have tried following

protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Insert into monthcal values (@NoOfMonths,@StartDate,@EndDate)", con);
cmd.Parameters.AddWithValue("@NoOfMonths", txtNoOfMonths.Text.ToString());
cmd.Parameters.AddWithValue("@StartDate", txtStartDate.Text.ToString());
cmd.Parameters.AddWithValue("@EndDate", txtEndDate.Text.ToString());
con.Open();
cmd.ExecuteNonQuery();

}
protected void txtStartDate_TextChanged(object sender, EventArgs e)
{
DateTime startDate = DateTime.Parse(txtStartDate.Text);

startDate.AddMonths(txtNoOfMonths.Text);
txtEndDate.Text = startDate.ToString("yyyy-MM-dd");
}
}

------------------

startDate.AddMonths(txtNoOfMonths.Text);


am getting error there as
Error 3 Argument 1: cannot convert from 'string' to 'int'
Error 2 The best overloaded method match for 'System.DateTime.AddMonths(int)' has some invalid arguments
Posted

1 solution

Start by converting your user inputs to the appropriate datatypes:
C#
int months;
if (!int.Parse(txtNoOfMonths.Text, out months))
   {
   // Report problem with number!
   return;
   }
DateTime startDate;
if (!DateTime.Parse(txtStartDate.Text, out startDate))
   {
   // Report problem with Date!
   return;
   }
Then use those values:
C#
DateTime endDate = startDate.AddMonths(months);
txtEndDate.Text = endDate>ToString();

And use the converted values instead of string values to pass directly to SQL.
(And please, don't say "But my database is all NVARCHAR" - because if it is, that needs to be fixed first!)
 
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