In .aspx file:
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false">
<AlternatingRowStyle BackColor="green" />
<Columns>
<asp:HyperLinkField HeaderText="Name" DataTextField="PName"
DataNavigateUrlFields="pname"
DataNavigateUrlFormatString="Sales.aspx?pname={0}" />
<asp:BoundField HeaderText="MinQty" DataField="MinOrdQty" />
<asp:BoundField HeaderText="MaxQty" DataField="MaxOrdQty" />
<asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice" />
</Columns>
</asp:GridView>
In .aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
string strcn = "Data source=name-of-sql-server;initial catalog=database-name;integrated security=sspi";
SqlConnection cn = new SqlConnection(strcn);
SqlCommand cmd = new SqlCommand("select pname,minordqty,MaxOrdQty,UnitPrice from dbo.products", cn);
try
{
cn.Open();
DataSet ds = new DataSet();
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(ds);
DataTable dt = ds.Tables[0];
//Session["ds"] = ds;
gvProducts.DataSource = dt;
gvProducts.DataBind();
}
catch (SqlException ex)
{
}
finally
{
cn.Close();
}
}
In Details page (Sales.aspx) :
<asp:GridView ID="gvSales" runat="server"></asp:GridView>
In Sales.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["pname"] != null)
{
string productName = Request.QueryString["pname"].ToString();
if (productName != null && productName != string.Empty)
DisplaySalesDetails(productName);
}
}
private void DisplaySalesDetails(string pname)
{
string strcn = "Data source=ADMIN-PC\\SQLEXPRESS;initial catalog=adomaterial;integrated security=sspi";
SqlConnection cn = new SqlConnection(strcn);
string qry=@"select * from dbo.sales where pid in(select pid from products
where pname='";
qry+=pname+"');";
SqlCommand cmd = new SqlCommand(qry,cn);
try
{
cn.Open();
DataSet ds = new DataSet();
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(ds);
Session["ds"] = ds;
gvSales.DataSource = ds;
gvSales.DataBind();
}
catch (SqlException ex)
{
}
finally
{
cn.Close();
}
}
Table structure:
Products table:
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false">
<AlternatingRowStyle BackColor="green" />
<Columns>
<asp:HyperLinkField HeaderText="Name" DataTextField="PName"
DataNavigateUrlFields="pname"
DataNavigateUrlFormatString="Sales.aspx?pname={0}" />
<asp:BoundField HeaderText="MinQty" DataField="MinOrdQty" />
<asp:BoundField HeaderText="MaxQty" DataField="MaxOrdQty" />
<asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice" />
</Columns>
</asp:GridView>
In .aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
string strcn = "Data source=name-of-sql-server;initial catalog=database-name;integrated security=sspi";
SqlConnection cn = new SqlConnection(strcn);
SqlCommand cmd = new SqlCommand("select pname,minordqty,MaxOrdQty,UnitPrice from dbo.products", cn);
try
{
cn.Open();
DataSet ds = new DataSet();
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(ds);
DataTable dt = ds.Tables[0];
//Session["ds"] = ds;
gvProducts.DataSource = dt;
gvProducts.DataBind();
}
catch (SqlException ex)
{
}
finally
{
cn.Close();
}
}
In Details page (Sales.aspx) :
<asp:GridView ID="gvSales" runat="server"></asp:GridView>
In Sales.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["pname"] != null)
{
string productName = Request.QueryString["pname"].ToString();
if (productName != null && productName != string.Empty)
DisplaySalesDetails(productName);
}
}
private void DisplaySalesDetails(string pname)
{
string strcn = "Data source=ADMIN-PC\\SQLEXPRESS;initial catalog=adomaterial;integrated security=sspi";
SqlConnection cn = new SqlConnection(strcn);
string qry=@"select * from dbo.sales where pid in(select pid from products
where pname='";
qry+=pname+"');";
SqlCommand cmd = new SqlCommand(qry,cn);
try
{
cn.Open();
DataSet ds = new DataSet();
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(ds);
Session["ds"] = ds;
gvSales.DataSource = ds;
gvSales.DataBind();
}
catch (SqlException ex)
{
}
finally
{
cn.Close();
}
}
Table structure:
Products table:
Pid | PName | Mid | MinOrdQty | MaxOrdQty | Active | UnitPrice |
---|---|---|---|---|---|---|
1 | colgate | 1 | 50 | 500 | 65 | |
2 | DoveSoap | 1 | 100 | 1000 | 25 | |
3 | DoveShampoo | 1 | 50 | 500 | 150 | |
4 | ParachuteOil | 2 | 20 | 50 | 80 | |
5 | RinSoap | 2 | 50 | 500 | 15 | |
6 | SurfExcel | 1 | 50 | 500 | 55 | |
7 | VIM | 2 | 10 | 100 | 15 | |
8 | EXO | 3 | 10 | 150 | 35 | |
9 | Medimix | 3 | 25 | 150 | 25 | |
10 | Pears | 4 | 10 | 200 | 26 | |
11 | NoteBook | 4 | 51 | 270 | 48 | |
12 | Cinthol | 20 | 300 | 23 | ||
13 | fiama | 4 | 20 | 200 | 67 |
Sales table:
Sid | Qty | OriginalPrice | TotalPrice | Discount | Active | SaleDate | Pid |
---|---|---|---|---|---|---|---|
1 | 10 | 1350 | 1200 | 150 | 23-Dec-11 12:00:00 AM | 3 | |
2 | 25 | 2000 | 1800 | 200 | 21-Aug-11 12:00:00 AM | 4 | |
3 | 20 | 1300 | 1000 | 300 | 13-Jun-10 12:00:00 AM | 1 | |
4 | 150 | 5500 | 4900 | 600 | 23-Dec-09 12:00:00 AM | 6 | |
5 | 50 | 1300 | 1100 | 200 | 02-Jun-08 12:00:00 AM | 10 | |
6 | 15 | 375 | 350 | 25 | 01-Feb-12 12:00:00 AM | 2 | |
7 | 25 | 3375 | 3100 | 275 | 02-Feb-11 12:00:00 AM | 3 | |
8 | 200 | 3000 | 2500 | 500 | 07-Oct-10 12:00:00 AM | 5 | |
9 | 150 | 3300 | 3200 | 100 | 29-Jul-11 12:00:00 AM | 9 | |
10 | 100 | 3500 | 3200 | 300 | 15-Nov-10 12:00:00 AM | 8 | |
11 | 10 | 550 | 500 | 50 | 02-Feb-11 12:00:00 AM | 6 | |
12 | 50 | 1100 | 1000 | 100 | 11-Aug-09 12:00:00 AM | 9 | |
13 | 80 | 1200 | 950 | 250 | 23-Dec-10 12:00:00 AM | 7 | |
14 | 100 | 2500 | 900 | 400 | 20-Oct-11 12:00:00 AM | 9 | |
15 | 120 | 1600 | 1500 | 100 | 03-Oct-12 12:00:00 AM | 7 |