Hi makenzi.exc,
Database
--EXEC Vegetables_GetVegetablesDetailsByPrice 90
CREATE PROCEDURE [Vegetables_GetVegetablesDetailsByPrice]
@PricePerKg INT
AS
BEGIN
SET NOCOUNT ON;
IF (@PricePerKg > 10 AND @PricePerKg < 40)
BEGIN
SELECT [ID]
,[Name]
,[PricePerKg]
,[Quantity]
FROM [Vegetables]
WHERE [PricePerKg] BETWEEN 10 AND 40
END
IF (@PricePerKg > 41 AND @PricePerKg < 70)
BEGIN
SELECT [ID]
,[Name]
,[PricePerKg]
,[Quantity]
FROM [Vegetables]
WHERE [PricePerKg] BETWEEN 41 AND 70
END
ELSE
BEGIN
SELECT [ID]
,[Name]
,[PricePerKg]
,[Quantity]
FROM [Vegetables]
END
END
HTML
<asp:DropDownList ID="ddlPricePerKg" runat="server">
<asp:ListItem Text="Please Select" Value="0"></asp:ListItem>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
<asp:ListItem Text="20" Value="20"></asp:ListItem>
<asp:ListItem Text="30" Value="30"></asp:ListItem>
<asp:ListItem Text="40" Value="40"></asp:ListItem>
<asp:ListItem Text="50" Value="50"></asp:ListItem>
<asp:ListItem Text="60" Value="60"></asp:ListItem>
<asp:ListItem Text="70" Value="70"></asp:ListItem>
<asp:ListItem Text="80" Value="80"></asp:ListItem>
<asp:ListItem Text="90" Value="90"></asp:ListItem>
<asp:ListItem Text="100" Value="100"></asp:ListItem>
</asp:DropDownList>
<asp:Button Text="Submit" runat="server" OnClick="OnSubmit" />
<hr />
<asp:GridView ID="gvVegetables" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="PricePerKg" HeaderText="PricePerKg" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
</Columns>
</asp:GridView>
Namespace
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
Code
#region Events
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnSubmit(Object sender, EventArgs e)
{
this.BindGrid();
}
#endregion
#region Private Methods
/// <summary>
/// populate GridView
/// </summary>
private void BindGrid()
{
int price = int.Parse(ddlPricePerKg.SelectedValue);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection sqlConnection = new SqlConnection(constr))
{
using (SqlCommand sqlCommand = new SqlCommand("Vegetables_GetVegetablesDetailsByPrice", sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@PricePerKg", price);
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
using (DataTable dtVegetables = new DataTable())
{
sqlDataAdapter.Fill(dtVegetables);
gvVegetables.DataSource = dtVegetables;
gvVegetables.DataBind();
}
}
}
}
}
#endregion