Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > dataset help, adding a column inside a loop

Reply
Thread Tools

dataset help, adding a column inside a loop

 
 
dSchwartz
Guest
Posts: n/a
 
      02-27-2004
I need help adding a column to a dataset, but its a little bit more
complicated then just that. Here's the situation: I have many xml
files in one directory, each which represent a newsletter. I want to
create a page that will display a title and date of each newsletter,
along with a link to it. here's a code snip:

=============================

DirectoryInfo dirInfo = new
DirectoryInfo(Server.MapPath("/newsletters/xml/"));
FileInfo[] newsletters = dirInfo.GetFiles("*.xml");
DataSet objDataSet = new DataSet();
string strVirtualPath = "";
foreach (FileInfo xfile in newsletters)
{ strVirtualPath = "/smartmail/xml/" + xfile;
objDataSet.ReadXml(Request.MapPath(strVirtualPath) );
articleList.DataSource = objDataSet.Tables;
articleList.DataBind();

DataView objDataView = new DataView(objDataSet.Tables[0]);
objDataView.Sort = "title";

articleValues.DataSource = objDataView;
articleValues.DataBind();
}

=================================

this produces a table (bound to datagrid) with one row for each xml
file. the columns are newsletter_ID, title, and date, which is good,
but i want to add a column that will contain the actual filename of
the xml file. How could this be accomplished?

Thanks for your time!


I don't think it will be needed to help me, but just in case, here is
a sample of the xml files:
=======================================
<?xml version="1.0" encoding="UTF-8"?>

<newsletter title="newsletter title" date="28 Feb 2004">
<article>
<title>specific article title</title>
<author>DJS</author>
<date>Jan 6 2004</date>
<body>specific article text here.</body>
</article>
<article>
.....
</article>
</newsletter>
========================================
 
Reply With Quote
 
 
 
 
Martin Dechev
Guest
Posts: n/a
 
      02-27-2004
Try the following:

> DirectoryInfo dirInfo = new
> DirectoryInfo(Server.MapPath("/newsletters/xml/"));
> FileInfo[] newsletters = dirInfo.GetFiles("*.xml");
> DataSet objDataSet = new DataSet();
> string strVirtualPath = "";
> foreach (FileInfo xfile in newsletters)
> { strVirtualPath = "/smartmail/xml/" + xfile;
> objDataSet.ReadXml(Request.MapPath(strVirtualPath) );


objDataSet.Tables[0].Columns.Add(
new System.Data.DataColumn("File Name", typeof(string)));
//it should have only one row, so we don't need to loop
//foreach(DataRow dr in objDataSet.Tables[0].Rows)
objDataSet.Tables[0].Rows[0][dr.Columns.Count - 1] = xfile;

> articleList.DataSource = objDataSet.Tables;
> articleList.DataBind();
>
> DataView objDataView = new DataView(objDataSet.Tables[0]);
> objDataView.Sort = "title";
>
> articleValues.DataSource = objDataView;
> articleValues.DataBind();
> }



 
Reply With Quote
 
 
 
 
dSchwartz
Guest
Posts: n/a
 
      03-01-2004
Thanks Martin,
I tried this out and I get an error:
"A Column named 'name_of_file' already belongs to this DataTable."

I figured that this was because the Columns.Add line is inside a loop
so I tried to hard code only one xml file to use and it worked fine
(got rid of the loop). I tried to move the Columns.Add line outside
of the loop but I think it needs to be below the ReadXml line?

Maybe someone could point out an easier way to loop through several
xml files in a directory and populating a dataset with 2 elements from
each file along with the filename?

Thanks for your time!



"Martin Dechev" <(E-Mail Removed)> wrote in message news:<uBFqh0W$(E-Mail Removed)>...
> Try the following:
>
> > DirectoryInfo dirInfo = new
> > DirectoryInfo(Server.MapPath("/newsletters/xml/"));
> > FileInfo[] newsletters = dirInfo.GetFiles("*.xml");
> > DataSet objDataSet = new DataSet();
> > string strVirtualPath = "";
> > foreach (FileInfo xfile in newsletters)
> > { strVirtualPath = "/smartmail/xml/" + xfile;
> > objDataSet.ReadXml(Request.MapPath(strVirtualPath) );

>
> objDataSet.Tables[0].Columns.Add(
> new System.Data.DataColumn("File Name", typeof(string)));
> //it should have only one row, so we don't need to loop
> //foreach(DataRow dr in objDataSet.Tables[0].Rows)
> objDataSet.Tables[0].Rows[0][dr.Columns.Count - 1] = xfile;
>
> > articleList.DataSource = objDataSet.Tables;
> > articleList.DataBind();
> >
> > DataView objDataView = new DataView(objDataSet.Tables[0]);
> > objDataView.Sort = "title";
> >
> > articleValues.DataSource = objDataView;
> > articleValues.DataBind();
> > }

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
copying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N ASP .Net 2 10-31-2003 01:05 PM
Ccopying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N ASP .Net Datagrid Control 2 10-31-2003 02:43 AM
Ccopying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N ASP .Net 1 10-31-2003 02:39 AM



Advertisments