Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have No of months and Start date and end date in my page

when i give number of months 3 and start date 1/6/2015 then end date will calculated to 1/9/2015 automatically

i want to create extra columns in database i.e June-2015, july and august
in this table or another table

my code is

protected void Button1_Click(object sender, EventArgs e)
       SqlCommand cmd = new SqlCommand("Insert into  monthcal (NoOfMonths,StartDate,EndDate) 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());

   protected void txtStartDate_TextChanged(object sender, EventArgs e)
       string inputString = txtStartDate.Text;
       DateTime dt = DateTime.ParseExact(inputString, "yyyy/MM/dd", CultureInfo.InvariantCulture);
       dt = dt.AddMonths(Convert.ToInt32(txtNoOfMonths.Text));
       txtEndDate.Text = dt.ToString("yyyy/MM/dd");

       DateTime Date1 = Convert.ToDateTime(txtStartDate.Text);
       DateTime Date2 = Convert.ToDateTime(txtEndDate.Text);
       //int Datediff = ((Date2.Year - Date1.Year) * 12) + Date1.Month - Date2.Month;
       int DayDiff = (Date2.Date - Date1.Date).Days;
       Label1.Text = "Total days" + " " + (DayDiff.ToString());
Updated 2-Jun-15 1:48am
Maciej Los 2-Jun-15 8:25am    
Don't do that!

Add rows instead of columns!

1 solution

use a calculated column:

Create table monthcal
  NoOfMonths int not null,
  StartDate date not null,
  EndDate as DateAdd (mm,NoOfMonths, Startdate)


or in your case:
alter table monthcal
drop EndDate 

alter table monthcal
add EndDate as dateadd(mm,1,getdate())
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