View Single Post
  #2 (permalink)  
Old December 21st, 2004, 05:58 AM
nashnash nashnash is offline
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to nashnash
Default

Hi,
Please copy and pase this code and this will give you the result as you need
-----------------------------save this as 'nested.aspx'--------

<%@ Import Namespace="System.Data" %>
<%@ Page language="c#" Codebehind="nested.aspx.cs" AutoEventWireup="false" Inherits="Insight.nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>nested</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:repeater id="parentRepeater" runat="server">


                <HeaderTemplate>
                    <table border="1" width="300" >
                        <tr>
                            <td width="100"><b>Size</b></td>
                            <td width="100"><b>Stock</b></td>
                            <td width="100"><b>Price</b></td>
                        </tr>
                    </table>
                </HeaderTemplate>
                <ItemTemplate>
                    <table border=1 width="300">
                        <tr>
                            <td width="100"><b><%# DataBinder.Eval(Container.DataItem,"Brand") %></b></td>
                            <td width="200" align=center ><b><%# DataBinder.Eval(Container.DataItem,"Code") %></b></td>
                        </tr>
                        </table>
                                <asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows ("myrelation") %>' runat="server">
                                <ItemTemplate>
                                    <table border=1 width="300">
                                    <tr>
                                        <td width="100"><%# DataBinder.Eval(Container.DataItem, "[size]")%></td>

                                        <td width="100"><%#CheckStock(Convert.ToInt32(DataBind er.Eval(Container.DataItem, "[stock]")))%></td>

                                        <td width="100"><%# DataBinder.Eval(Container.DataItem, "[price]")%></td>
                                    </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:repeater>
                        </td>
                    </tr>

            </ItemTemplate>
            </asp:repeater>
        </form>
    </body>
</HTML>

----------------------
------------Save this as 'nested.aspx.cs'----------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Insight
{
    /// <summary>
    /// NASIM aHMAD
    /// 21/12/2004
    /// </summary>
    public class nested : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Repeater parentRepeater;

        private void Page_Load(object sender, System.EventArgs e)
        {
            SqlConnection cnn = new SqlConnection("server=INSIGHT;UID=sa;PWD=Insight;d atabase=Pubs;"); //cHANGE YOUR CONNECTION STRING HERE
            SqlDataAdapter cmd1 = new SqlDataAdapter("select * from product",cnn);

            //Create and fill the DataSet.
            DataSet ds = new DataSet();
            cmd1.Fill(ds,"product");

            //Create a second DataAdapter for the Stock table.
            SqlDataAdapter cmd2 = new SqlDataAdapter("select * from stock",cnn);
            cmd2.Fill(ds,"stock");

            //Create the relation bewtween the product and stock tables.
            ds.Relations.Add("myrelation",ds.Tables["product"].Columns["prod_id"],ds.Tables["stock"].Columns["prod_id"]);

            //Bind the product table to the parent Repeater control, and call DataBind.
            parentRepeater.DataSource = ds.Tables["product"];
            Page.DataBind();

            //Close the connection.
            cnn.Close();
        }

        // This function will take the stock and
        // retrun you the label if there is Stock
        // it will print Stock other wise it will
        // print 'No Stock'
        protected string CheckStock(int Stock)
        {
            if(Stock>0)
            {
                return Stock.ToString();
            }

            else
            {
                return "No Stock";
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}
-------------------

Please change your connection string here
SqlConnection cnn = new SqlConnection("server=INSIGHT;UID=sa;PWD=Insight;d atabase=Pubs;"); //cHANGE YOUR CONNECTION STRING HERE

in "nested.aspx.cs"

i have kept the table name as 'product' and 'stock' and same column name as you sepcified...

Please let me know if it doesnt work for you.


Thanks
Nash