Link to home
Start Free TrialLog in
Avatar of allanmark
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_Nested' 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.NullReferenceException: 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(Object sender, EventArgs e) +22
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +98
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +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_connection=yes;database=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(dsAuthTitles, "Authors");
         daTitles.Fill(dsAuthTitles, "Titles");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     string sPathFile = @"D:\Mystuff - New Horizons\Training\AuthorTitles.xml";
     DataColumn parentCol = dsAuthTitles.Tables["Authors"].Columns["au_id"];
     DataColumn childCol  = dsAuthTitles.Tables["Titles"].Columns["au_id"];
     DataRelation drAuthTitle = new DataRelation("AuthorTitles", parentCol, childCol);
     dsAuthTitles.Relations.Add(drAuthTitle);
     dsAuthTitles.WriteXml(sPathFile, 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!
Avatar of stormist
stormist

I believe you have to instantiate an XMLwriter instance?
XmlTextWriter textWriter = new XmlTextWriter(sPathFile, null) ;
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of allanmark

ASKER

Thanks for an excellent reply!