Hierarchical Data Binding in ASP.NET

This will show a sample of hierarchical data bound to
hierarchical List Controls (repeaters)

 

This is just a transcript of the great article by Fritz Onion
from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspn-hierdatabinding.asp

 

Get the data

This is obvious

 

//Create DataSet and Bind it to the
repeater

private
void Page_Load(object sender, EventArgs e)

{

     
string strConn
=

            “server=.;trusted_connection=yes;database=northwind”;

 

     
string strSql 
= “SELECT CustomerID, CompanyName FROM ” +

           
” Customers;
”                         +

           
“SELECT OrderID, CustomerID,
”         +

           
”        EmployeeID FROM
Orders;”      +

            “SELECT OrderID,
Products.ProductID,”  +

            “ProductName, Products.UnitPrice
FROM” +

            ” [Order Details], Products WHERE
”    +

           
” [Order Details].ProductID =
”        +

            “Products.ProductID”;

 

      SqlConnection conn = new SqlConnection(strConn);

      SqlDataAdapter da = new SqlDataAdapter(strSql, conn);

      da.TableMappings.Add(“Customers1”,
“Orders”);

      da.TableMappings.Add(“Customers2”,
“OrderDetails”);

 

     
//Fill DataSet

      _ds = new DataSet();

      da.Fill(_ds, “Customers”);

 

     
//Add Relations

      _ds.Relations.Add(“Customer_Order”,

            _ds.Tables[“Customers”].Columns[“CustomerID”],

            _ds.Tables[“Orders”].Columns[“CustomerID”]);

      _ds.Relations[0].Nested = true;

      _ds.Relations.Add(“Order_OrderDetail”,

            _ds.Tables[“Orders”].Columns[“OrderID”],

            _ds.Tables[“OrderDetails”].Columns[“OrderID”]);

      _ds.Relations[1].Nested = true;

 

     
//Bind

      _customerRepeater.DataSource =
_ds.Tables[“Customers”];

      _customerRepeater.DataBind();

}

 

Very useful function

///<summary>

/// returns a DataView of ChildRows

///</summary>

///<param name=”dataItem”>parent row</param>

///<param name=”relation”>the name of the
relation
</param>

///<returns>DataView</returns>

protected DataView GetChildRelation(object dataItem,

     
string relation)

{

      DataRowView drv = dataItem as DataRowView;

     
if (drv != null)

           
return drv.CreateChildView(relation);

     
else

           
return
null;

}

 

HTML view

Three hierarchical repeaters:

 

<asp:RepeaterRunat=”server”ID=”_customerRepeater”

     
EnableViewState=”false”>

 
<ItemTemplate>

    Customer:

   
<%#
DataBinder.Eval(Container.DataItem, “CustomerID”) %>

  
&nbsp;
&nbsp;

   
<%#
DataBinder.Eval(Container.DataItem,”CompanyName”) %>

   
<br/>

   
<asp:Repeaterrunat=”server”EnableViewState=”false”

        
DataSource=

           
<%# GetChildRelation(Container.DataItem,

                                 
“Customer_Order”)%>

   
ID=”Repeater1″>

     
<itemTemplate>

      
&nbsp;&nbsp;&nbsp;&nbsp;

       Orderid:<b>

      
<%#DataBinder.Eval(Container.DataItem,
“OrderID”)%>

      
</b><br/>

      
<asp:Repeaterrunat=”server”EnableViewState=”false”

           
DataSource=

               
<%#
GetChildRelation(Container.DataItem,

                                    
“Order_OrderDetail”)%>

      
ID=”Repeater2″>

        
<itemTemplate>

          
&nbsp;&nbsp;&nbsp;&nbsp;

          
&nbsp;&nbsp;&nbsp;&nbsp;

          
<b><%# DataBinder.Eval(Container.DataItem,

                                 
“ProductName”) %></b>

           $<%#
DataBinder.Eval(Container.DataItem,

                   
            “UnitPrice”) %>
<br/>

        
</itemTemplate>

      
</asp:Repeater>

    
</itemTemplate>

  
</asp:Repeater>

 </ItemTemplate>

</asp:Repeater>

Small development Q&A

Q:how to zip in .NET
A:find library ICSharpCode

Q:System.Net.HttpWebRequest vs WebClient

Q:How to create self-signed ssl certificate?
A:use makecert.exe (part of .NET) Ex:makecert.exe -a SHA1 -ss my -sr LocalMachine -n "CN=ComputerName" -b 01/01/2000 -e 01/01/2050 -eku 1.3.6.1.5.5.7.3.1 -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12
Then install it in IIS

Q:How to implement ssl switching (b/w http & https) in asp.net app?
A:See "Switching Between HTTP and HTTPS Automatically" by "Matt Sollars"

Q:How to create multiple Web Servers in WinXP?
A:cscript c:\Inetpub\Adminscripts\adsutil.vbs COPY W3SVC/1 W3SVC/2
http://www.bobshowto.com/iis_servers.htm

Q: how to copy user’s profile?
A: http://support.microsoft.com/default.aspx?scid=kb;EN-US;811151

Q: SELECT permission denied on object ‘ASPStateTempApplications’, database ‘tempdb’, owner ‘dbo’.
A: http://support.microsoft.com/default.aspx?scid=kb;en-us;810474
http://idunno.org/dotNet/sessionState.aspx

Q: How to debug Client-Side in .NET
A: -In IE Tools\Adv Options clear "Disable script debugging…"
-In VS.NET Tools\Debug Processes attach to the IExplore.exe
-In VS.NET set a breakpoint in the Client Side code
-In IE cause the script to run

Q: When connecting another computer to cable modem, it can’t connect to Internet saying "Unable to contact DHCP host"
A:Reboot the cable modem

Q:aspx files don’t run, but open "File download" dialog.
A:C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\aspnet_regiis.exe -i

Q: When trying to run asp.net, the server gives: "Server Application Unavailable" error
A:http://support.microsoft.com/default.aspx?scid=kb;en-us;Q315158

Q: Disable Windows Explorer search assitant
A: In HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\CabinetState add String Value "Use Search Asst" with value "no"

Q: How to output an xls file
A: Response.ContentType = "application/vnd.ms-excel" OR "application/excel" and maybe Page.EnableViewState = "false"