Check datepicker for no value or null value

8 posts, 1 answers
  1. Kevin
    Kevin avatar
    265 posts
    Member since:
    Jun 2011

    Posted 17 Apr 2012 Link to this post

    I am tring to save information based on if they pick a date back inot the Db.  So I am checking on the selectedvalue of the raddatepicker but it gives me this error when I do not have a date in there.

    Bassically what I want to do is rotate through my radgrid and if they have picked a date then save information to the DB otherwise just go to the next row.  But this error pops if no date is picked.

    Nullable object must have a value.

    Line 238:            If TempDt.SelectedDate.Value > Nothing Then


    Protected Sub lnkSaveTemp_Click(sender As Object, e As System.EventArgs) Handles lnkSaveTemp.Click 
          For Each row As GridDataItem In myRadGrid.Items 
              Dim Id As Integer = row.GetDataKeyValue("intIssuedId") 
              Dim TempDt As RadDatePicker = DirectCast(row.FindControl("DatePicker"), RadDatePicker) 
              Dim TNote As TextBox = DirectCast(row.FindControl("txtNotes"), TextBox) 
              If TempDt.SelectedDate.Value > Nothing Then 
                  sql = "Update Drat_Issued set dtRecoverTemp = '" & sanitizeString(TempDt.SelectedDate.Value) & "', strNotes = '" & sanitizeString(TNote.Text) & "' where intIssuedId = " & Id 
                  Response.Write(sql) 
                  insertUpdateDelete(sql) 
              End If 
          Next 
      End Sub
  2. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 17 Apr 2012 Link to this post

    Hello Kevin,

    Please check below code snippet.
    RadDatePicker rdp1 = new RadDatePicker();
            if (rdp1.SelectedDate != null && rdp1.SelectedDate.GetValueOrDefault() != DateTime.MinValue)
            {
                                       // do your functionality here
            }


    OR

    Dim rdp1 As New RadDatePicker()
     
     
    If rdp1.SelectedDate IsNot Nothing AndAlso rdp1.SelectedDate.GetValueOrDefault() <> DateTime.MinValue Then
    End If


    Thanks,
    Jayesh Goyani
  3. huejiitech
    huejiitech avatar
    8 posts
    Member since:
    Aug 2013

    Posted 21 Sep 2013 Link to this post

    How about code in javascript?
  4. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 23 Sep 2013 Link to this post

    Hello,

    Can you please elaborate your scenario?

    Thanks,
    Jayesh Goyani
  5. Matt
    Matt avatar
    93 posts
    Member since:
    Jun 2012

    Posted 19 Jun 2018 in reply to Jayesh Goyani Link to this post

    Here is one...

    function ClientSubmit(sender, args) {

    var tdate = $find("<%=RDP_TerminationDate.ClientID %>");
    var rdate = $find("<%=RDP_ResignationDate.ClientID %>");

    if (tdate.get_selectedDate() == null && rdate.get_selectedDate() == null) {
    radalert("Please select a termination or resignation date.<br/><br/>Thank you.", 300, 180, "Attention", null, null);
    if (tdate.get_selectedDate() == null && rdate.get_selectedDate() != null) {
    tdate.showPopup();
    }
    else {
    rdate.showPopup();
    }
    return args.set_cancel(true);
    }

    }

    Either one of these fields are NOT null however, the control thinks it is...

     

  6. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4656 posts

    Posted 20 Jun 2018 Link to this post

    Hello Matt,

    The following seems to work fine for me and below is attached a video that shows the expected behavior. Could you take a look at my test and see whether I am missing something? If so, can you show me what it is and what the problematic scenario is, so I can take a look?

    <telerik:RadDatePicker runat="server" ID="RDP_TerminationDate"></telerik:RadDatePicker>
    <telerik:RadDatePicker runat="server" ID="RDP_ResignationDate"></telerik:RadDatePicker>
    <telerik:RadButton runat="server" ID="rb1" OnClientClicking="ClientSubmit" Text="Submit"></telerik:RadButton>
    <telerik:RadWindowManager runat="server" ID="rwm1"></telerik:RadWindowManager>
    <script>
        function ClientSubmit(sender, args) {
            var tdate = $find("<%=RDP_TerminationDate.ClientID %>");
            var rdate = $find("<%=RDP_ResignationDate.ClientID %>");
            alert(tdate.get_selectedDate());
            alert(rdate.get_selectedDate());
            //you may want to use a logical OR operator instead of AND, depending on the logic that is desired
            //for example:
            //if (tdate.get_selectedDate() == null || rdate.get_selectedDate() == null) {
            if (tdate.get_selectedDate() == null && rdate.get_selectedDate() == null) {
                radalert("Please select a termination or resignation date.<br/><br/>Thank you.", 300, 180, "Attention", null, null);
                //you may want to execute this logic in the callback function of the radalert
                if (tdate.get_selectedDate() == null && rdate.get_selectedDate() != null) {
                    tdate.showPopup();
                }
                else {
                    rdate.showPopup();
                }
                args.set_cancel(true);
            }
        }
    </script>

     


    Regards,
    Marin Bratanov
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  7. Matt
    Matt avatar
    93 posts
    Member since:
    Jun 2012

    Posted 10 Jul 2018 in reply to Marin Bratanov Link to this post

    Thanks (sorry for the late response) but the user is required to select either the tdate or the rdate. Either one is acceptable. That's why I check to see if they both are empty.

    The problem was that get_selectedDate() would not show/capture the date that I had selected.

  8. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4656 posts

    Posted 11 Jul 2018 Link to this post

    Hello Matt,

    In such a case, the OR operator is perfect, I just mentioned this because the majority of cases I come across validation requires both fields.

    If you still have issues with this scenario, could you post here the Telerik controls version that the project is using and the relevant markup+JavaScript that causes the problem so I can take a look? You can modify my previous snippet if that's easier for you than extracting the actual problem.


    Regards,
    Marin Bratanov
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top