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

rajeshsachdevaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.