Validating StartDate and EndDate - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Validating StartDate and EndDate

Last post 12-02-2008 05:47 AM by Raajkumar. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 12-02-2008 01:29 AM

    Validating StartDate and EndDate

    Hi, I have created an infopath form in which I placed two datepicker control.

    I need to validate like both the startdate month & year ,enddate month&year should be same

    How can do this? I tried using the C# code behind like this,

    public void End_Validating(object sender, XmlValidatingEventArgs e)

    {

    XPathNavigator pathNav = this.MainDataSource.CreateNavigator();
    string strStartDate = pathNav.SelectSingleNode("/my:TimeSheet/my:Dates/my:start", NamespaceManager).Value.ToString();
    string StrEndDate = pathNav.SelectSingleNode("/my:TimeSheet/my:Dates/my:End", NamespaceManager).Value.ToString();
    if (e.Operation == XmlOperation.ValueChange)
    {
    if (strStartDate != "" || StrEndDate != "")
    {
    iSDate = Convert.ToInt32(strStartDate.Substring(5, 2).ToString());
    iEDate = Convert.ToInt32(StrEndDate.Substring(5, 2).ToString());
    if (iSDate iEDate)
    {
    e.ReportError(e.Site, true, "Please select the same month");
    }
    }
    }
    }
    but it is showing error ,

    System.ArgumentOutOfRangeException
    startIndex cannot be larger than length of string.
    Parameter name: startIndex
    at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
    at TimeSheet.FormCode.End_Validating(Object sender, XmlValidatingEventArgs e)
    at Microsoft.Office.InfoPath.Internal.XmlEventHost.GenericProxy(Object genericDelegate, DataDOMEvent dataDOMEvent, InfoPathEvents type)
    at Microsoft.Office.InfoPath.Internal.XmlEventHost.ValidatingProxy(DataDOMEvent dataDOMEvent)
    at Microsoft.Office.Interop.InfoPath.SemiTrust._DataDOMEventSink_SinkHelper.OnValidate(DataDOMEvent pDataDOMEvent)



    Please Help me
  • 12-02-2008 02:09 AM In reply to

    Re: Validating StartDate and EndDate

    Hi,

    You can directly validate using the data validation. No need to use the code.

    1. Go to the date picker properties.

    2. Click on data validation.

    3. Click Add.

    4. First date picker is not blank and

    5. Second date picker is not blank and

    6. Select the first date picker field in first drop down, not equal to in second drop down, second date picker field in third drop down. And enter the screen tip and message which you want to show to the user.

    7. Click ok.

    Swathip
    www.ggktech.com
  • 12-02-2008 02:21 AM In reply to

    Re: Validating StartDate and EndDate

    Hi,

    Thanks for you reply.

    I tried but according to the solution, both the dates should be same like 12/2/2008 == 12/2/2008 if it is like 12/2/2008 and 12/30/2008 it is showing error

    I need to check only the year and month not the date
  • 12-02-2008 04:12 AM In reply to

    Re: Validating StartDate and EndDate

    Hi,

    Your code is working correctly for me.

    Put condition like this. if (iSDate != iEDate)Are you using the same code which you have given? I think at the substring indexes you went wrong. Once check.

    Hope this helps.

    Rajitha
  • 12-02-2008 04:24 AM In reply to

    Re: Validating StartDate and EndDate

    Hi,

    The code works if I am going through debugging mode but if i go through by clicking preview in infopath IDE then it is showing error
  • 12-02-2008 04:31 AM In reply to

    Re: Validating StartDate and EndDate

    It looks like this line might be the problem:

        if (strStartDate != "" || StrEndDate != "")

    you should perform the comparison if both are valid dates, not if just one is, so it should be an && operation.  Also, your code could be a bit more robust.  Give this a try:

         XPathNavigator pathNav = this.MainDataSource.CreateNavigator();
        
    string strStartDate = pathNav.SelectSingleNode("/my:TimeSheet/my:Dates/my:start", NamespaceManager).Value.ToString();
         string strEndDate = pathNav.SelectSingleNode("/my:TimeSheet/my:Dates/my:End", NamespaceManager).Value.ToString();
         if (e.Operation == XmlOperation.ValueChange)
         {
              DateTime startDateObj, endDateObj;
              if (DateTime.TryParse(strStartDate, out startDateObj) && DateTime.TryParse(strEndDate, out endDateObj))
              {
                   if (startDateObj.Year != endDateObj.Year || startDateObj.Month != endDateObj.Month)
                   {
                        e.ReportError(e.Site,
    true, "Please select the same month");
                   }
              }
         }

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 12-02-2008 05:06 AM In reply to

    Re: Validating StartDate and EndDate

    Hi,

    it is working now

    Thanks to every one who have helped me in sorting this problem!
  • 12-02-2008 05:47 AM In reply to

    Re: Validating StartDate and EndDate

    Hi all,

    There is problem when I activated this to a Sharepoint.

    I have made this form to be browser enabled

    The validation not working in sharepoint Please Help Me!!
Page 1 of 1 (8 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.