i'm developing a faq session of our website and I'm facing some
problems handling xml files and DataSet relations
First off all, here's the structure of my xml file:
<?xml version="1.0"?>
<questions>
<topic order="1" title="General" >
<item order="1" question="blabl abla?">
<answer>blablab la</answer>
</item>
<item order="2" question="blabl abla?">
<answer>blablab la</answer>
</item>
</topic>
<topic order="2" title="Police Office">
<item order="1" question="blabl abla?">
<answer>blablab la</answer>
</item>
</topic>
<topic order="2" title="Ensuranc e">
<item order="1" question="blabl abla?">
<answer>blablab la</answer>
</item>
<item order="2" question="blabl abla?">
<answer>blablab la</answer>
</item>
<item order="3" question="blabl abla?">
<answer>blablab la</answer>
</item>
</topic>
and so on...This structure is acceptable? I'm forgetting something? I
know it doesnt have an schema, but I havent learned how to build one
yet.
Now the code
I have two datagrids, as the MS sample (in
http://support.microsoft.com/default...b;en-us;308485)
but I have changed something to simplify
private void Page_Load(objec t sender, System.EventArg s e)
{ // Put user code to initialize the page here
string filename = Server.MapPath( ".");
XmlDocument doc = new XmlDocument();
doc.Load(filena me + "\\perguntas.xm l");
// this function reads the xml file and create 3 tables (topics, itens
and answers) whithin a dataset ds
ds = ReadXml(doc);
ds.Relations.Ad d("rel", ds.Tables["topics"].Columns["order"],
ds.Tables["itens"].Columns["order"]);
grid1.DataSourc e = ds;
grid1.DataMembe r = "topics";
grid1.DataKeyFi eld = "order";
grid1.DataBind( ); }
up to here, everything is ok, I can load the xml file and show the
first level of it (the <topic) tag's colection with a '+' at the left
side where I want the child elements (in this case the <itens> tags)
Now the problem. When I click in the '+' column the event
OnSelectedIndex Changed is fired.
//delegate for OnSelectedIndex Changed of Grid1
protected void grid1_OnSelecte dIndexChanged(S ystem.Object sender,
System.EventArg s e) {
this.Showdetail grid(); }
Following, the function that handles the event (where the error come
out)
// handle the event
protected void Showdetailgrid( ) {
if(grid1.Select edIndex != -1)
{
DataView parentrows = new DataView();
DataView childrows;
DataRowView currentparentro w;
parentrows.Tabl e = ds.Tables["itens"];
currentparentro w = parentrows[grid1.SelectedI ndex];
childrows = currentparentro w.CreateChildVi ew("rel"); ***
grid2.DataSourc e = childrows;
grid2.DataBind( );
grid2.Visible = true;
}
else
grid2.Visible = false;
}
When the line with *** is executed I got the following error:
System.Argument Exception: The relation is not parented to the table to
which this DataView points.
Could someone help me please?