This is a migrated thread and some comments may be shown as answers.

Check datepicker for no value or null value

7 Answers 2562 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 17 Apr 2012, 01:48 PM
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

7 Answers, 1 is accepted

Sort by
1
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Apr 2012, 02:18 PM
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
0
huejiitech
Top achievements
Rank 1
answered on 21 Sep 2013, 04:32 PM
How about code in javascript?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Sep 2013, 05:35 AM
Hello,

Can you please elaborate your scenario?

Thanks,
Jayesh Goyani
0
Matt
Top achievements
Rank 1
answered on 19 Jun 2018, 05:43 PM

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...

 

0
Marin Bratanov
Telerik team
answered on 20 Jun 2018, 11:19 AM
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.
0
Matt
Top achievements
Rank 1
answered on 10 Jul 2018, 07:56 PM

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.

0
Marin Bratanov
Telerik team
answered on 11 Jul 2018, 04:13 PM
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.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
huejiitech
Top achievements
Rank 1
Matt
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or