rajeshsachdeva
asked on
Help with Calculating the date
Hi
I need help calculating the end date based on the number of weeks entered. I am using datagrid.Please see the code below:
I am keep getting error. Also, I am not so sure whether shud I do this on the client side or do it in the code behind. Any help is appreicated.
I need help calculating the end date based on the number of weeks entered. I am using datagrid.Please see the code below:
I am keep getting error. Also, I am not so sure whether shud I do this on the client side or do it in the code behind. Any help is appreicated.
<asp:TemplateColumn HeaderText="Start Date">
<ItemTemplate>
<peterCC:DateTextBox ID="txtDateStart" runat="server" xEnableContextMenuB="False" Width="75px" >
<xPopupCalendar xToggleImageUrl="PetersDatePackage/Appearance/Calendar.jpg" > </xPopupCalendar>
</peterCC:DateTextBox>
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="# Weeks">
<ItemTemplate>
<asp:TextBox ID="txNoWeeks" runat="server" Text="" Width="30px" MaxLength="4" AutoPostBack="true"
OnTextChanged="TextBox_Changed" ></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="End Date">
<ItemTemplate>
<peterCC:DateTextBox ID="txtDateEnd" runat="server" xEnableContextMenuB="False" Width="75px" >
<xPopupCalendar xToggleImageUrl="PetersDatePackage/Appearance/calendar.jpg" > </xPopupCalendar>
</peterCC:DateTextBox>
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
In my code behind I am doing the following:
protected void TextBox_Changed(object sender, EventArgs e)
{
int intWeeks = 0;
foreach (DataGridItem item in dgPhaseDisplay.Items)
{
TextBox txtWeeks = (TextBox)item.FindControl("txNoWeeks");
DateTime txtEndDate = (DateTime)item.FindControl("txtDateEnd");
DateTime txtStartDate = (DateTime)item.FindControl("txtDateStart");
if (intWeeks < 0)
{
SetValidationErrorMessage("Please enter the numeric value in the number of weeks.");
}
else
{
int intNumDays = 0;
intNumDays = Convert.ToInt32(txtWeeks) * 7;
txtEndDate = Convert.ToDateTime(txtStartDate).AddDays(14).ToString("d");
}
}
}
What is the error you are seeing?
Jim
Jim
it's hard to say before we see the error, but you can check if your data conversion is happening in the same format all along your code. Like, if I'm using UK date-time format and my project is set to US-English by default, it may be failing to convert something like 31/12/2008.
Cheers,
Yurich
Cheers,
Yurich
I see several problems in here. Can you tell me what kind of controls are:
txtDateEnd and txtDateStart?
Jim
txtDateEnd and txtDateStart?
Jim
Try changing the line
txtEndDate = Convert.ToDateTime(txtStar tDate).Add Days(14).T oString("d ");
to this...
txtEndDate.Text = Convert.ToDateTime(txtStar tDate.Text ).AddDays( 14).ToStri ng("d");
txtEndDate = Convert.ToDateTime(txtStar
to this...
txtEndDate.Text = Convert.ToDateTime(txtStar
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all your help. I have another question, If I want to perform the same action in Javascript, can I do Registe.ClientScript.... or do something else. Please Help
Thanks
Thanks
ASKER
Hi Jim
I tried your code and getting the following errors:
Error 1 Cannot convert null to 'System.DateTime' because it is a value type
Error 2 Cannot implicitly convert type 'string' to 'System.DateTime'
Error 3 The name 'txtDateEnd' does not exist in the current context
Any help is appreicated.
My Code:
protected void TextBox_Changed(object sender, EventArgs e)
{
int intWeeks = 0;
foreach (DataGridItem item in dgPhaseDisplay.Items)
{
TextBox txtWeeks = (TextBox)item.FindControl( "txNoWeeks ");
TextBox endDateTB = (TextBox)item.FindControl( "txtDateEn d");
TextBox startDateTB = (TextBox)item.FindControl( "txtDateSt art");
if (startDateTB == null)
throw new Exception("Unable to locate startDate textbox.");
if (endDateTB == null)
throw new Exception("Unable to locate endDate textbox.");
DateTime txtStartDate = DateTime.Parse(startDateTB .Text);
DateTime endDate = null;
if ((txtWeeks == null) || !int.TryParse(txtWeeks.Tex t, out intWeeks))
{
SetValidationErrorMessage( "Please enter the numeric value in the number of weeks.");
}
else
{
endDate = Convert.ToDateTime(txtStar tDate).Add Days(intWe eks * 7).ToString("d");
txtDateEnd.Text = endDate.ToString();
}
}
}
--ASP.Net Code:
<asp:datagrid ID="dgPhaseDisplay" HeaderStyle-Font-Size="x-s mall" HeaderStyle-Font-Bold="Tru e" HeaderStyle-VerticalAlign= "Bottom"
AlternatingItemStyle-CssCl ass="small color1" ItemStyle-VerticalAlign="T op"
CellPadding="1" CellSpacing="1" HeaderStyle-BackColor="Lig htBlue" BorderStyle="None" GridLines="None"
PagerStyle-VerticalAlign=" Top" AllowSorting="True" AutoGenerateColumns="False " runat="server" ItemStyle-Wrap="True" >
<AlternatingItemStyle CssClass="smallcolor1" ></AlternatingItemStyle>
<ItemStyle CssClass="smallcolor2" VerticalAlign="Top" Wrap="True"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Bold="True" VerticalAlign="Bottom" BackColor="LightBlue"></He aderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="dgChkBox" runat="server" Checked="false" />
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:BoundColumn DataField="lnkPhaseID" Visible="False" HeaderText="Phase ID" SortExpression="lnkPhaseID "></asp:Bo undColumn>
<asp:BoundColumn DataField="lnkPhaseDescp" HeaderText="Phase Descp" SortExpression="lnkPhaseDe scp">
<ItemStyle Width="250px" />
<FooterStyle Font-Size="X-Small" />
<HeaderStyle Width="250px" />
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Start Date">
<ItemTemplate>
<peterCC:DateTextBox ID="txtDateStart" runat="server" xEnableContextMenuB="False " Width="75px" >
<xPopupCalendar xToggleImageUrl="PetersDat ePackage/A ppearance/ Calendar.j pg" > </xPopupCalendar>
</peterCC:DateTextBox>
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="# Weeks">
<ItemTemplate>
<asp:TextBox ID="txNoWeeks" runat="server" Text="" Width="30px" MaxLength="4" AutoPostBack="true"
OnTextChanged="TextBox_Cha nged" ></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="End Date">
<ItemTemplate>
<peterCC:DateTextBox ID="txtDateEnd" runat="server" xEnableContextMenuB="False " Width="75px" >
<xPopupCalendar xToggleImageUrl="PetersDat ePackage/A ppearance/ calendar.j pg" > </xPopupCalendar>
</peterCC:DateTextBox>
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Alias Descp">
<ItemTemplate>
<asp:TextBox ID="txtPhaseAlias" runat="server" Text=""></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Top" />
</asp:datagrid>
</td>
</tr>
I tried your code and getting the following errors:
Error 1 Cannot convert null to 'System.DateTime' because it is a value type
Error 2 Cannot implicitly convert type 'string' to 'System.DateTime'
Error 3 The name 'txtDateEnd' does not exist in the current context
Any help is appreicated.
My Code:
protected void TextBox_Changed(object sender, EventArgs e)
{
int intWeeks = 0;
foreach (DataGridItem item in dgPhaseDisplay.Items)
{
TextBox txtWeeks = (TextBox)item.FindControl(
TextBox endDateTB = (TextBox)item.FindControl(
TextBox startDateTB = (TextBox)item.FindControl(
if (startDateTB == null)
throw new Exception("Unable to locate startDate textbox.");
if (endDateTB == null)
throw new Exception("Unable to locate endDate textbox.");
DateTime txtStartDate = DateTime.Parse(startDateTB
DateTime endDate = null;
if ((txtWeeks == null) || !int.TryParse(txtWeeks.Tex
{
SetValidationErrorMessage(
}
else
{
endDate = Convert.ToDateTime(txtStar
txtDateEnd.Text = endDate.ToString();
}
}
}
--ASP.Net Code:
<asp:datagrid ID="dgPhaseDisplay" HeaderStyle-Font-Size="x-s
AlternatingItemStyle-CssCl
CellPadding="1" CellSpacing="1" HeaderStyle-BackColor="Lig
PagerStyle-VerticalAlign="
<AlternatingItemStyle CssClass="smallcolor1" ></AlternatingItemStyle>
<ItemStyle CssClass="smallcolor2" VerticalAlign="Top" Wrap="True"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Bold="True" VerticalAlign="Bottom" BackColor="LightBlue"></He
<Columns>
<asp:TemplateColumn HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="dgChkBox" runat="server" Checked="false" />
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:BoundColumn DataField="lnkPhaseID" Visible="False" HeaderText="Phase ID" SortExpression="lnkPhaseID
<asp:BoundColumn DataField="lnkPhaseDescp" HeaderText="Phase Descp" SortExpression="lnkPhaseDe
<ItemStyle Width="250px" />
<FooterStyle Font-Size="X-Small" />
<HeaderStyle Width="250px" />
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Start Date">
<ItemTemplate>
<peterCC:DateTextBox ID="txtDateStart" runat="server" xEnableContextMenuB="False
<xPopupCalendar xToggleImageUrl="PetersDat
</peterCC:DateTextBox>
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="# Weeks">
<ItemTemplate>
<asp:TextBox ID="txNoWeeks" runat="server" Text="" Width="30px" MaxLength="4" AutoPostBack="true"
OnTextChanged="TextBox_Cha
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="End Date">
<ItemTemplate>
<peterCC:DateTextBox ID="txtDateEnd" runat="server" xEnableContextMenuB="False
<xPopupCalendar xToggleImageUrl="PetersDat
</peterCC:DateTextBox>
</ItemTemplate>
<ItemStyle Width="75px" />
<HeaderStyle Width="75px" />
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Alias Descp">
<ItemTemplate>
<asp:TextBox ID="txtPhaseAlias" runat="server" Text=""></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateColumn>
</Columns>
<PagerStyle VerticalAlign="Top" />
</asp:datagrid>
</td>
</tr>
ASKER
Jim
I solved the problem.
Thanks for your help. You guys are the best.
Just FYI.. Take a look at the code:
protected void TextBox_Changed(object sender, EventArgs e)
{
int intWeeks = 0;
foreach (DataGridItem item in dgPhaseDisplay.Items)
{
TextBox txtWeeks = (TextBox)item.FindControl( "txNoWeeks ");
TextBox endDateTB = (TextBox)item.FindControl( "txtDateEn d");
TextBox startDateTB = (TextBox)item.FindControl( "txtDateSt art");
if (startDateTB == null)
throw new Exception("Unable to locate startDate textbox.");
if (endDateTB == null)
throw new Exception("Unable to locate endDate textbox.");
if (startDateTB.Text != "")
{
DateTime txtStartDate = DateTime.Parse(startDateTB .Text);
if ((txtWeeks == null) || !int.TryParse(txtWeeks.Tex t, out intWeeks))
{
SetValidationErrorMessage( "Please enter the numeric value in the number of weeks.");
}
else
{
int numberofdays = 0;
switch (txtStartDate.DayOfWeek)
{
case DayOfWeek.Monday:
{
numberofdays = 3 + (intWeeks * 7);
break;
}
case DayOfWeek.Tuesday:
{
numberofdays = 3 + (intWeeks * 7);
break;
}
case DayOfWeek.Wednesday:
{
numberofdays = 3 + (intWeeks * 7);
break;
}
case DayOfWeek.Thursday:
{
numberofdays = 4 + (intWeeks * 7);
break;
}
case DayOfWeek.Friday:
{
numberofdays = 3 + (intWeeks * 7);
break;
}
case DayOfWeek.Saturday:
{
numberofdays = 2 + (intWeeks * 7);
break;
}
case DayOfWeek.Sunday:
{
numberofdays = 1 + (intWeeks * 7);
break;
}
}
string strEndDate = Convert.ToDateTime(txtStar tDate).Add Days(numbe rofdays).T oString("d ");
endDateTB.Text = strEndDate.ToString();
}
}
}
}
I solved the problem.
Thanks for your help. You guys are the best.
Just FYI.. Take a look at the code:
protected void TextBox_Changed(object sender, EventArgs e)
{
int intWeeks = 0;
foreach (DataGridItem item in dgPhaseDisplay.Items)
{
TextBox txtWeeks = (TextBox)item.FindControl(
TextBox endDateTB = (TextBox)item.FindControl(
TextBox startDateTB = (TextBox)item.FindControl(
if (startDateTB == null)
throw new Exception("Unable to locate startDate textbox.");
if (endDateTB == null)
throw new Exception("Unable to locate endDate textbox.");
if (startDateTB.Text != "")
{
DateTime txtStartDate = DateTime.Parse(startDateTB
if ((txtWeeks == null) || !int.TryParse(txtWeeks.Tex
{
SetValidationErrorMessage(
}
else
{
int numberofdays = 0;
switch (txtStartDate.DayOfWeek)
{
case DayOfWeek.Monday:
{
numberofdays = 3 + (intWeeks * 7);
break;
}
case DayOfWeek.Tuesday:
{
numberofdays = 3 + (intWeeks * 7);
break;
}
case DayOfWeek.Wednesday:
{
numberofdays = 3 + (intWeeks * 7);
break;
}
case DayOfWeek.Thursday:
{
numberofdays = 4 + (intWeeks * 7);
break;
}
case DayOfWeek.Friday:
{
numberofdays = 3 + (intWeeks * 7);
break;
}
case DayOfWeek.Saturday:
{
numberofdays = 2 + (intWeeks * 7);
break;
}
case DayOfWeek.Sunday:
{
numberofdays = 1 + (intWeeks * 7);
break;
}
}
string strEndDate = Convert.ToDateTime(txtStar
endDateTB.Text = strEndDate.ToString();
}
}
}
}
show me the lines where you get those errors?
also make sure you do have a textbox named txtDateEnd
also make sure you do have a textbox named txtDateEnd
ASKER
Now how can I do the same using Javascript?????
Sorry - I have not used JS in years.
Jim
Jim
ASKER
Thanks
ASKER