Click here to Skip to main content
15,942,964 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i entered a date range, but datepicker is taking default date(current date as assigned) when seraching a data.

What I have tried:

VB
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
     If Not Me.IsPostBack Then
         PickerFrom.SelectedDate = Today
         PickerTo.SelectedDate = Today
         'Me.Search()
         'Grid.DataBind()
     End If
 End Sub

 Private Sub Search()
     Dim strWhere As String = ""
     strWhere = "orderdate between '" & PickerFrom.SelectedDate.Value.ToString("MM/dd/yyyy") & "' and '" & PickerTo.SelectedDate.Value.AddDays(1).AddSeconds(-1).ToString("MM/dd/yyyy") & "' and OID=" & CStr(Filter.OrganisationId)
     Dim SqlCommand As New SqlCommand
     Dim connection As String = ConfigurationManager.ConnectionStrings("VService").ConnectionString
     Dim con As New SqlConnection(connection)
     SqlCommand.CommandText = "Custom_Analysers_PartSales_Freight"
     SqlCommand.Parameters.Add("@Type", SqlDbType.VarChar).Direction = ParameterDirection.Input
     SqlCommand.Parameters("@Type").Value = "Partsales"
     SqlCommand.Parameters.Add("@whereclause", SqlDbType.VarChar).Direction = ParameterDirection.Input
     SqlCommand.Parameters("@whereclause").Value = strWhere
     SqlCommand.CommandTimeout = 0
     SqlCommand.Connection = con
     SqlCommand.CommandType = CommandType.StoredProcedure
     Dim sqlDA As New SqlDataAdapter(SqlCommand)
     Dim ds As New DataSet
     sqlDA.Fill(ds)
     Grid.DataSource = ds.Tables(0).DefaultView
 End Sub

 Protected Sub Grid_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles Grid.NeedDataSource
     If PickerFrom.SelectedDate.HasValue Then
         Me.Search()
     End If
 End Sub

 Protected Sub Grid_PageIndexChanged(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridPageChangedEventArgs) Handles Grid.PageIndexChanged
     Grid.CurrentPageIndex = e.NewPageIndex
 End Sub

 Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
     Me.Search()
     Grid.DataBind()
 End Sub





<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Assembly="DotNetSmith.WebControls" Namespace="DotNetSmith.WebControls" TagPrefix="dns" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="ajax" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">   
   <p>
      <dns:DotNetSmithUserMessageSummary ID="Umsg" runat="server" CssClass="MessageBlockError" />
   </p>
   <table cellspacing="4" cellpadding="0" border="0" width="100%">
        <tr>           
            <td align="right"><font color="red">All amounts are exclusive of tax</font></td>
        </tr>
   </table>
   <fieldset id="fdsExceptionTracking" runat="server">
   <legend>Parts Sales Analyser</legend>   
            <table cellspacing="4" cellpadding="0" border="0">
                <tr>
                <td align="right" width="35%">Select Date Range:</td>
                <td>
                    <telerik:RadDatePicker ID="PickerFrom" runat="server" Width="140px" DateInput-DateFormat="dd/MM/yyyy" SkinID="CustomRadDatePickerSkin"
                            DateInput-EmptyMessage="MinDate" MinDate="01/01/1000" MaxDate="01/01/3000">
                            <Calendar ID="Calendar1" runat="server">
                                <SpecialDays>
                                    <telerik:RadCalendarDay Repeatable="Today" ItemStyle-CssClass="rcToday" />
                                </SpecialDays>
                            </Calendar>
                        </telerik:RadDatePicker>
                </td>
                <td>To</td>
                <td>
                    <telerik:RadDatePicker ID="PickerTo" runat="server" Width="140px" DateInput-DateFormat="dd/MM/yyyy" SkinID="CustomRadDatePickerSkin"
                            DateInput-EmptyMessage="MaxDate" MinDate="01/01/1000" MaxDate="01/01/3000">
                            <Calendar ID="Calendar2" runat="server">
                                <SpecialDays>
                                    <telerik:RadCalendarDay Repeatable="Today" ItemStyle-CssClass="rcToday"  />
                                </SpecialDays>
                            </Calendar>
                    </telerik:RadDatePicker>
                </td>                
                <td><asp:Button ID="btnSearch" runat="server" Text="Search" /></td>
                </tr>        
            </table>
Posted
Updated 5-Sep-18 9:19am
v2
Comments
Richard Deeming 5-Sep-18 11:58am    
strWhere = "orderdate between '" & PickerFrom.SelectedDate.Value.ToString("MM/dd/yyyy") & "' and '" & PickerTo.SelectedDate.Value.AddDays(1).AddSeconds(-1).ToString("MM/dd/yyyy") & "' and OID=" & CStr(Filter.OrganisationId)


Don't do it like that! Your code is almost certainly vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

It seems your stored procedure is using dynamic SQL without parameterizing it. You need to use sp_executesql[^] with a properly parameterized query.
[no name] 5-Sep-18 12:50pm    
Ok Sir, but can above solution solve my problem of holding value in datepicker control.
[no name] 6-Sep-18 7:38am    
Can there is any another solution, please?
[no name] 6-Sep-18 8:41am    
Did you checked if your control retains the value you assigned when "SEARCH" is called. You defaulted the value for date controls to Today on Pre-render.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900