Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 10th, 2007, 12:04 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default Dynamic XML Manipulation Discussion

Hello everyone,
    I don't have a question that needs answered in this thread but just would like some comments on the solution that I can up with for a particular requirement that was handed to me earlier today.

Basically, there is a stored procedure attached to one of our SQL Servers that returns different amounts and types of data based upon the input parameter (A client id) and management wanted to see this displayed web based and not inside of Crystal Reports. So begins my morning.

Initially I thought I could just bind this result set to a control and be done with it, unfortunately, my requirement was specific in how it needed to be displayed. The stored procedure returns a resultset that contains 2 columns: Category and Objective and the display should be as such:

[Category 1]
        Objective 1
        Objective 2
[Category 2]
        Objective 1
        Objective 2

And so on. Problem was is that my data was returned to me in the stored procedure like this:
[Category 1] [Objective 1]
[Category 1] [Objective 2]
[Category 2] [Objective 1]
[Category 2] [Objective 2]

I sat and thought about this and thought about this but there was no way (that I could think of) to transform this data inside a control based on the output of the stored procedure.

Obviously the way that management wanted this displayed is hierarchial and XML is great at storing hierarchial data but then came the next hurdle: I couldn't write anything to disk and everything had to be done in code. =
Basically this is what I came up with:

foreach(DataRow dr in dt.Rows)
            {
                if(sCategory == "")
                {
                    sCategory = Convert.ToString(dr["ProblemDescription"]);
                    sXml += "<section><description>" + sCategory + "</description><objective>";
                }
                else if(sCategory != "" && sCategory != Convert.ToString(dr["ProblemDescription"]))
                {
                    sCategory = Convert.ToString(dr["ProblemDescription"]);
                    sXml += "</objective></section><section>";
                    sXml += "<description>" + sCategory + "</description><objective>";

                }
                sXml += Convert.ToString(dr["objective"]);
            }
            sXml += "</objective></section></txplan>";

            //Response.Write(sXml);
            StringReader rdr = new StringReader(sXml);
            ds.ReadXml(rdr);

            foreach(DataTable t in ds.Tables)
            {
                foreach(DataRow dr in t.Rows)
                {
                    dr["objective"] = Convert.ToString(dr["objective"]).Replace("[BEGIN]", "<li class='noMsg'>");
                    dr["objective"] = Convert.ToString(dr["objective"]).Replace("[END]", "</li>");
                }
            }
            this.rprHistory.DataSource = ds;
            this.rprHistory.DataBind();

Inside of the repeater, my data is displayed as was required by the initial requirement.

What I am wondering is if there is a more effective way to do something like this when the data has to be pulled directly from SQL and manipulated in code.

The one question I did have was with this line:
sXml += "</objective></section></txplan>";

That line closes the element and container of the XML document but if i add a closing </xml> tag to that string, at run time I get an error saying that it is an unexpected end tag? (FYI, The sXml string does contain an opening XML tag)

Does the DataSet appened /xml to the string when I call .ReadXml?

Anyway, comments would be greatly appreciated. ^^

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
  #2 (permalink)  
Old April 11th, 2007, 08:46 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Parsons,

A bit busy, so couldn't go with the XML discussion, may be once I get free with some work here, I'll like to discuss that, but an alternate to this, a simple page that will display the data in a datalist is as below, created it in 5 mins, so plz do ask for issues:

-----------
CODEBEHIND
-----------
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;

namespace WebApplication155
{
    /// <summary>
    /// Summary description for wrox.
    /// </summary>
    public class wrox : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataList dlCategory;
        string sCategory = string.Empty;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if(!IsPostBack)
                BindGrid();
        }

        private void BindGrid()
        {
            DataTable dt = new DataTable(); ;
            DataRow dr;

            dt.Columns.Add(new DataColumn("Category", typeof(string)));
            dt.Columns.Add(new DataColumn("Objective", typeof(string)));

            dr = dt.NewRow();
            dr[0] = "[Category1]";
            dr[1] = "[Objective1]";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "[Category1]";
            dr[1] = "[Objective2]";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "[Category2]";
            dr[1] = "[Objective1]";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "[Category2]";
            dr[1] = "[Objective2]";
            dt.Rows.Add(dr);

            dlCategory.DataSource = dt;
            dlCategory.DataBind();


        }

        #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.dlCategory.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler (this.dlCategory_ItemDataBound);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void dlCategory_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            Label lblCatg;
            HtmlTableRow trCatg;

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                lblCatg = (Label) e.Item.FindControl("lblCategory");
                trCatg = (HtmlTableRow) e.Item.FindControl("trCategory");

                //sCategory is the publicly declared variable
                if (sCategory == lblCatg.Text)
                {
                    trCatg.Style.Add("display","none");
                }

                sCategory = lblCatg.Text;

            }
        }
    }
}


-----
HTML
-----
<%@ Page language="c#" Codebehind="wrox.aspx.cs" AutoEventWireup="false" Inherits="WebApplication155.wrox" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>wrox</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <table cellSpacing="0" cellPadding="0" width="100%">
                <tr>
                    <td vAlign="top"><asp:datalist id="dlCategory" Runat="server">
                            <ItemTemplate>
                                <table width="100%" cellpadding="0" cellspacing="0">
                                    <tr id="trCategory" runat="server">
                                        <td>
                                            <asp:Label Runat=server ID="lblCategory" text='<%#DataBinder.Eval(Container.DataItem,"Categ ory")%>'>
                                            </asp:Label>
                                        </td>
                                    </tr>
                                    <tr id="trObjective" runat="server">
                                        <td style="PADDING-LEFT: 20px">
                                            <asp:Label Runat=server ID="lblObjective" text='<%#DataBinder.Eval(Container.DataItem,"Objec tive")%>'>
                                            </asp:Label>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:datalist></td>
                </tr>
            </table>
        </form>
    </body>
</HTML>




Regards
Mike

Fortune favours the brave, so don't regret on missed oppurtunities.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic XML Scripts IrishJoker XSLT 2 August 30th, 2006 05:33 AM
Crystal Reports with dynamic XML kncd2005 Crystal Reports 0 May 27th, 2005 12:11 PM
Dynamic XML site NeilS21 XML 1 April 18th, 2005 07:35 PM
Dynamic Email --> XML MadMaxMan XML 2 June 4th, 2003 04:04 PM



All times are GMT -4. The time now is 04:48 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.