[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

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.


<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"); 
 
            }
        }    
         
    }

Open in new window

0
rajeshsachdeva
Asked:
rajeshsachdeva
1 Solution
 
rajeshsachdevaAuthor Commented:
Please Help!
0
 
JimBrandleyCommented:
What is the error you are seeing?

Jim
0
 
YurichCommented:
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
JimBrandleyCommented:
I see several problems in here. Can you tell me what kind of controls are:
txtDateEnd and txtDateStart?

Jim
0
 
Erick37Commented:
Try changing the line
txtEndDate = Convert.ToDateTime(txtStartDate).AddDays(14).ToString("d");

to this...
txtEndDate.Text = Convert.ToDateTime(txtStartDate.Text).AddDays(14).ToString("d");
0
 
JimBrandleyCommented:
Based on your control names, I'm assuming they are all textboxes. If that's true, the following should work.

Jim

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("txtDateEnd");
            TextBox startDateTB = (TextBox)item.FindControl("txtDateStart");
 
            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.Text, out intWeeks))
            {
                SetValidationErrorMessage("Please enter the numeric value in the number of weeks.");
            }
            else
            {
                endDate = Convert.ToDateTime(txtStartDate).AddDays(intWeeks * 7).ToString("d");
                txtDateEnd.Text = endDate.ToString();
            }
        }    
         
    }

Open in new window

0
 
rajeshsachdevaAuthor Commented:
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
0
 
rajeshsachdevaAuthor Commented:
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("txtDateEnd");
            TextBox startDateTB = (TextBox)item.FindControl("txtDateStart");
 
            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.Text, out intWeeks))
            {
                SetValidationErrorMessage("Please enter the numeric value in the number of weeks.");
            }
            else
            {
                endDate = Convert.ToDateTime(txtStartDate).AddDays(intWeeks * 7).ToString("d");
                txtDateEnd.Text = endDate.ToString();
            }
        }    
    }

--ASP.Net Code:

<asp:datagrid  ID="dgPhaseDisplay" HeaderStyle-Font-Size="x-small" HeaderStyle-Font-Bold="True" HeaderStyle-VerticalAlign="Bottom"
                            AlternatingItemStyle-CssClass="smallcolor1" ItemStyle-VerticalAlign="Top"
                            CellPadding="1" CellSpacing="1" HeaderStyle-BackColor="LightBlue" 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"></HeaderStyle>
                            <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>
                                  <asp:BoundColumn DataField="lnkPhaseDescp" HeaderText="Phase Descp" SortExpression="lnkPhaseDescp">
                            <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="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>
                                  <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>
0
 
rajeshsachdevaAuthor Commented:
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("txtDateEnd");
            TextBox startDateTB = (TextBox)item.FindControl("txtDateStart");
 
            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.Text, 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(txtStartDate).AddDays(numberofdays).ToString("d");
                    endDateTB.Text = strEndDate.ToString();
               }
           }
        }    
    }
0
 
YZlatCommented:
show me the lines where you get those errors?

also make sure you do have a textbox named txtDateEnd
0
 
rajeshsachdevaAuthor Commented:
Now how can I do the same using Javascript?????
0
 
JimBrandleyCommented:
Sorry - I have not used JS in years.

Jim
0
 
rajeshsachdevaAuthor Commented:
Thanks
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now