ADO Net Database ASP.Net Tutorial

File: ShowMARS.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    void Page_Load()
    {
        if (!Page.IsPostBack)
            BuildTree();
    }
    void BuildTree()
    {
        string connectionString = @"MultipleActiveResultSets=True;"
            + @"Data Source=.\SQLExpress;Integrated Security=True;"
            + @"AttachDBFileName=|DataDirectory|MyDatabase.mdf;User Instance=True";
        SqlConnection con = new SqlConnection(connectionString);
        string cmdCategoriesText = "SELECT Id,Name FROM ProductCategories";
        SqlCommand cmdCategories = new SqlCommand(cmdCategoriesText, con);
        string cmdProductsText = "SELECT Title FROM Products "
            + "WHERE CategoryId=@CategoryID";
        SqlCommand cmdProducts = new SqlCommand(cmdProductsText, con);
        cmdProducts.Parameters.Add("@CategoryId", SqlDbType.Int);
        using (con)
        {
            con.Open();
            SqlDataReader categories = cmdCategories.ExecuteReader();
            while (categories.Read())
            {
                int id = categories.GetInt32(0);
                string name = categories.GetString(1);
                TreeNode catNode = new TreeNode(name);
                TreeView1.Nodes.Add(catNode);
                cmdProducts.Parameters["@CategoryId"].Value = id;
                SqlDataReader products = cmdProducts.ExecuteReader();
                while (products.Read())
                {
                    string title = products.GetString(0);
                    TreeNode productNode = new TreeNode(title);
                    catNode.ChildNodes.Add(productNode);
                }
                products.Close();
            }
        }
    }



    Show MARS


    
    

            id="TreeView1"
        Runat="server" />