Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a page that will produce data in a gridview that is triggered by a persons name and data between entries in a textbox with from date and a textbox with a to date.

my gridview SQL code reads as follows:

SELECT DATEDIFF(hh, log_in,log_out ) AS Total, officer_name, login_date, log_in, log_out FROM [Security_Log] WHERE ([officer_name] = @officer_name) AND [login_date] between ([login_date] =@from_date) AND ([login_date] = @to_date) AND (log_in IS NOT NULL) AND (log_out IS NOT NULL) AND (log_in !='') AND (log_out !='')

I can't figure out how to write the text box values into the between statement.

My Code is like this on the page:
XML
<asp:Button ID="ButtonReset" runat="server" CssClass="auto-style6" Text="Reset Page" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" DataSourceID="SqlDataSourceHoursWorked" GridLines="None">
    <Columns>
        <asp:BoundField DataField="officer_name" HeaderText="officer_name" SortExpression="officer_name" />
        <asp:BoundField DataField="login_date" HeaderText="login_date" SortExpression="login_date" />
        <asp:BoundField DataField="log_in" HeaderText="log_in" SortExpression="log_in" />
        <asp:BoundField DataField="log_out" HeaderText="log_out" SortExpression="log_out" />
        <asp:BoundField DataField="Total" HeaderText="Total" SortExpression="Total" ReadOnly="True" />
    </Columns>
    <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
    <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#594B9C" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceHoursWorked" runat="server" ConnectionString="<%$ ConnectionStrings:CorrectConnectionString %>" SelectCommand="SELECT DATEDIFF(hh, log_in,log_out ) AS Total, officer_name, login_date, log_in, log_out FROM [Security_Log] WHERE ([officer_name] = @officer_name) AND  [login_date] between ([login_date] =@from_date) AND  ([login_date] = @to_date) AND (log_in IS NOT NULL) AND (log_out IS NOT NULL) AND (log_in !='') AND (log_out !='')">
    <SelectParameters>
        <asp:ControlParameter ControlID="DDOfficerName" Name="officer_name" PropertyName="SelectedValue" />
        <asp:ControlParameter ControlID="TextBoxFromDate" Name="from_date" PropertyName="Text" />
        <asp:ControlParameter ControlID="TextBoxToDate" Name="to_date" PropertyName="Text" />
    </SelectParameters>
</asp:SqlDataSource>
</strong>
Posted
Comments
njammy 30-Jun-15 6:11am    
Are you getting some sort of error? or are you seeing unexpected grid view results as a result of the query? Please elaborate.

1 solution

SELECT DATEDIFF(hh, log_in,log_out ) AS Total, officer_name, login_date, log_in, log_out FROM [Security_Log] WHERE ([officer_name] = @officer_name) AND [login_date] between ([login_date] =convert(datetime,@from_date,103)) AND ([login_date] = convert(datetime,@to_date,103)) AND (log_in IS NOT NULL) AND (log_out IS NOT NULL) AND (log_in !='') AND (log_out !='')



Try With this code by passing textbox values as @from_date,@to_date
 
Share this answer
 

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