Hi @nauna,
Hi have made a small snippet for you.
Please have a look
SQL
CREATE PROCEDURE Students_GetStudents
@StartDate DATETIME
,@EndDate DATETIME
,@FilterBy INT = 1
AS
BEGIN
SET NOCOUNT ON;
IF @FilterBy = 1
BEGIN
SELECT Id
,Name
,City
,StartDate
,EndDate
,MAX(Marks) Marks
FROM Table_Students
WHERE StartDate = @StartDate
AND EndDate = @EndDate
GROUP BY Id
,Name
,City
,StartDate
,EndDate
END
ELSE
BEGIN
SELECT Id
,Name
,City
,StartDate
,EndDate
,MIN(Marks) Marks
FROM Table_Students
WHERE StartDate = @StartDate
AND EndDate = @EndDate
GROUP BY Id
,Name
,City
,StartDate
,EndDate
END
END
GO
HTML
<div>
<div>
Start Date:<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<br />
End Date:
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
Filter By:<asp:DropDownList ID="ddlFilterValues" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Max" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Min" Value="2"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="btnFilter" runat="server" Text="Filter" OnClick="Filter" />
</div>
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="StartDate" HeaderText="Start Date" />
<asp:BoundField DataField="EndDate" HeaderText="End Date" />
<asp:BoundField DataField="Marks" HeaderText="Marks" />
</Columns>
</asp:GridView>
</div>
C# Code
SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
cmd = new SqlCommand("SELECT Name,City,StartDate,EndDate,Marks FROM Table_Students",con);
this.gvStudents.DataSource = this.GetData(cmd);
this.gvStudents.DataBind();
}
}
protected void Filter(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtStartDate.Text.Trim()) && !string.IsNullOrEmpty(this.txtEndDate.Text.Trim()))
{
DateTime startDate = Convert.ToDateTime(this.txtStartDate.Text.Trim());
DateTime endDate = Convert.ToDateTime(this.txtEndDate.Text.Trim());
cmd = new SqlCommand("Students_GetStudents",con);
cmd.Parameters.AddWithValue("@StartDate",startDate);
cmd.Parameters.AddWithValue("@EndDate",endDate);
cmd.Parameters.AddWithValue("@FilterBy",this.ddlFilterValues.SelectedItem.Value);
cmd.CommandType = CommandType.StoredProcedure;
this.gvStudents.DataSource=this.GetData(cmd);
this.gvStudents.DataBind();
}
}
private DataTable GetData(SqlCommand cmd)
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
Namespaces
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;