allanmark
asked on
Error when attempting to write out XML file
Greetings!
Me again!!
I am in the process of trying to write out an XML file in what I thought was a straightforward few liner. The following error occurs when I run the app:
Server Error in '/XML_03_WriteXML_and_Nest ed' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
_Default.Button1_Click(Obj ect sender, EventArgs e) +22
System.Web.UI.WebControls. Button.OnC lick(Event Args e) +75
System.Web.UI.WebControls. Button.Rai sePostBack Event(Stri ng eventArgument) +98
System.Web.UI.WebControls. Button.Sys tem.Web.UI .IPostBack EventHandl er.RaisePo stBackEven t(String eventArgument) +7
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData) +33
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +4919
The code:
public partial class _Default : System.Web.UI.Page
{
protected SqlConnection sqlConnect;
protected SqlDataAdapter daAuthors;
protected SqlDataAdapter daTitles;
protected DataSet dsAuthTitles;
private string conString = (@"Server=MASK\SQLEXPRESS; trusted_co nnection=y es;databas e=Pubs");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string sqlStr = "select au_id, title, pubdate from titleauthor, titles ";
sqlStr = sqlStr + "where titleauthor.title_id = titles.title_id order by title";
sqlConnect = new SqlConnection(conString);
daAuthors = new SqlDataAdapter("select au_id, au_lname, au_fname from Authors", sqlConnect);
daTitles = new SqlDataAdapter(sqlStr, sqlConnect);
dsAuthTitles = new DataSet();
daAuthors.Fill(dsAuthTitle s, "Authors");
daTitles.Fill(dsAuthTitles , "Titles");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sPathFile = @"D:\Mystuff - New Horizons\Training\AuthorTi tles.xml";
DataColumn parentCol = dsAuthTitles.Tables["Autho rs"].Colum ns["au_id" ];
DataColumn childCol = dsAuthTitles.Tables["Title s"].Column s["au_id"] ;
DataRelation drAuthTitle = new DataRelation("AuthorTitles ", parentCol, childCol);
dsAuthTitles.Relations.Add (drAuthTit le);
dsAuthTitles.WriteXml(sPat hFile, XmlWriteMode.IgnoreSchema) ;
}
THIS IS NOT THE FIRST TIME I'VE HAD THIS TYPE OF ERROR. IS THERE SOME FUNDAMENTAL CONCEPT THAT I AM MISSING?
In advance3, tks 4 the help!
Me again!!
I am in the process of trying to write out an XML file in what I thought was a straightforward few liner. The following error occurs when I run the app:
Server Error in '/XML_03_WriteXML_and_Nest
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
_Default.Button1_Click(Obj
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
The code:
public partial class _Default : System.Web.UI.Page
{
protected SqlConnection sqlConnect;
protected SqlDataAdapter daAuthors;
protected SqlDataAdapter daTitles;
protected DataSet dsAuthTitles;
private string conString = (@"Server=MASK\SQLEXPRESS;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string sqlStr = "select au_id, title, pubdate from titleauthor, titles ";
sqlStr = sqlStr + "where titleauthor.title_id = titles.title_id order by title";
sqlConnect = new SqlConnection(conString);
daAuthors = new SqlDataAdapter("select au_id, au_lname, au_fname from Authors", sqlConnect);
daTitles = new SqlDataAdapter(sqlStr, sqlConnect);
dsAuthTitles = new DataSet();
daAuthors.Fill(dsAuthTitle
daTitles.Fill(dsAuthTitles
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sPathFile = @"D:\Mystuff - New Horizons\Training\AuthorTi
DataColumn parentCol = dsAuthTitles.Tables["Autho
DataColumn childCol = dsAuthTitles.Tables["Title
DataRelation drAuthTitle = new DataRelation("AuthorTitles
dsAuthTitles.Relations.Add
dsAuthTitles.WriteXml(sPat
}
THIS IS NOT THE FIRST TIME I'VE HAD THIS TYPE OF ERROR. IS THERE SOME FUNDAMENTAL CONCEPT THAT I AM MISSING?
In advance3, tks 4 the help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for an excellent reply!
XmlTextWriter textWriter = new XmlTextWriter(sPathFile, null) ;