找到你要的答案

Q:Display data from SQL based on date selected

Q:从SQL的基础上选定日期显示数据

I'm new to ASP.NET I have a GridView that display data from SQL table. I want to display data in the GridView based on the date selected from either Calendar or Textbox. Here is the code:

<p><asp:Label ID="Date" runat="server">Select Date:</asp:Label>

  <asp:Calendar ID="Calendar2" runat="server"  SelectionMode="DayWeekMonth" onselectionchanged="Calendar1_SelectionChanged">
        </asp:Calendar>

    <p>Todays Date is:  <asp:Label ID="lblday" runat="server"></asp:Label></p>
    <p>Selected Date is:<asp:Label ID="lblbday" runat="server"></asp:Label></p>

<asp:Button ID="addDates" runat="server" Text="Display" class="button1" 
        style="margin-left: 137px" Width="150px" />
    </p>
<!--<p><asp:Calendar ID="drdates" runat="server" class="textbox1"></asp:Calendar>-->


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" Height="178px" Width="402px">
        <Columns>
            <asp:BoundField DataField="scode" HeaderText="scode" SortExpression="scode" />
            <asp:BoundField DataField="drdates" HeaderText="drdates" 
                SortExpression="drdates" />
            <asp:BoundField DataField="Column1" HeaderText="Column1" ReadOnly="True" 
                SortExpression="Column1" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AbcConnectionString %>" 
        SelectCommand="select scode, drdates, count(*) from drreceive_20141229 group by drdates, scode">
    </asp:SqlDataSource>

Here is C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace ISYSProject
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            lblday.Text = Calendar2.TodaysDate.ToShortDateString();
            lblbday.Text = Calendar2.SelectedDate.ToShortDateString();
        }
    }
}

I'm new to ASP.NET I have a GridView that display data from SQL table. I want to display data in the GridView based on the date selected from either Calendar or Textbox. Here is the code:

<p><asp:Label ID="Date" runat="server">Select Date:</asp:Label>

  <asp:Calendar ID="Calendar2" runat="server"  SelectionMode="DayWeekMonth" onselectionchanged="Calendar1_SelectionChanged">
        </asp:Calendar>

    <p>Todays Date is:  <asp:Label ID="lblday" runat="server"></asp:Label></p>
    <p>Selected Date is:<asp:Label ID="lblbday" runat="server"></asp:Label></p>

<asp:Button ID="addDates" runat="server" Text="Display" class="button1" 
        style="margin-left: 137px" Width="150px" />
    </p>
<!--<p><asp:Calendar ID="drdates" runat="server" class="textbox1"></asp:Calendar>-->


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" Height="178px" Width="402px">
        <Columns>
            <asp:BoundField DataField="scode" HeaderText="scode" SortExpression="scode" />
            <asp:BoundField DataField="drdates" HeaderText="drdates" 
                SortExpression="drdates" />
            <asp:BoundField DataField="Column1" HeaderText="Column1" ReadOnly="True" 
                SortExpression="Column1" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AbcConnectionString %>" 
        SelectCommand="select scode, drdates, count(*) from drreceive_20141229 group by drdates, scode">
    </asp:SqlDataSource>

这是C #代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace ISYSProject
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            lblday.Text = Calendar2.TodaysDate.ToShortDateString();
            lblbday.Text = Calendar2.SelectedDate.ToShortDateString();
        }
    }
}
answer1: 回答1:

Do the following:

  1. On the SQLDataSource1 Datasource, go into [Properties] >> [Select Command] and click on "Add Parameter" and fill in details (Parameter source = "Control" and the rest is self explanatory).
  2. Update your SQL Query to include the parameter "..... drreceive_20141229 where drdate = @WhateverYouCalledParameter"
  3. Update your "Calendar1_SelectionChanged" and add "SQLDataSource1.databind();"

That should be it.

做以下:

  1. On the SQLDataSource1 Datasource, go into [Properties] >> [Select Command] and click on "Add Parameter" and fill in details (Parameter source = "Control" and the rest is self explanatory).
  2. Update your SQL Query to include the parameter "..... drreceive_20141229 where drdate = @WhateverYouCalledParameter"
  3. Update your "Calendar1_SelectionChanged" and add "SQLDataSource1.databind();"

那应该是它。

c#  sql  sql-server  gridview