Object reference not set to an instance of an object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Object reference not set to an instance of an object

Hybrid View

  1. #1
    Join Date
    Nov 2004
    Posts
    14

    Object reference not set to an instance of an object

    Thanks to the help I receive earlier, I'm making some progress with my code to generate an xml file based on records found in my database, but I'm stuck again:

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    Source Error:
    Line 41: root.AppendChild(mainNode);

    here's my new code:

    Code:
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Xml" %>
    <script runat="server">
      protected void Page_Load (object sender, EventArgs e) {
    		XmlDocument xmlDoc = new XmlDocument();
            XmlNode xn=xmlDoc.CreateElement("event");
            xmlDoc.AppendChild(xn);       
            using (SqlConnection conn = new SqlConnection(@"Data Source=mssql;Initial Catalog=xxxxx;User Id=xxxxx;Password=xxxxx;"))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select NewsNo, NewsEventDate, EventStartTime, EventEndTime, NewsHeadline, NewsDetails, NewsCategory from DailyNews where NewsCategory = 'event'", conn);
                XmlNode root= xmlDoc.SelectSingleNode("/events");
                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        XmlElement mainNode = xmlDoc.CreateElement("event");
                        XmlElement idNode= xmlDoc.CreateElement("NewsNo");
                        XmlElement eventDateNode= xmlDoc.CreateElement("NewsEventDate");
                        XmlElement startTimeNode= xmlDoc.CreateElement("EventStartTime");
    					XmlElement endTimeNode= xmlDoc.CreateElement("EventEndTime");
    					XmlElement eventNameNode= xmlDoc.CreateElement("NewsHeadline");
    					XmlElement eventDetailsNode= xmlDoc.CreateElement("NewsDetails");
                        idNode.InnerText = sdr.GetInt32(0).ToString();
                        eventDateNode.InnerText = sdr.GetDateTime(1).ToString();
                        startTimeNode.InnerText = sdr.GetString(2);
    					endTimeNode.InnerText = sdr.GetString(3);
    					eventNameNode.InnerText = sdr.GetString(4);
    					eventDetailsNode.InnerText = sdr.GetString(5);
                        mainNode.AppendChild(idNode);
                        mainNode.AppendChild(eventDateNode);
                        mainNode.AppendChild(startTimeNode);
    					mainNode.AppendChild(endTimeNode);
    					mainNode.AppendChild(eventNameNode);
    					mainNode.AppendChild(eventDetailsNode);
                        root.AppendChild(mainNode);             
                    }
                }
    			xmlDoc.Save("events.xml");
    		}
        }
    Thanks in advance!

  2. #2
    Join Date
    Nov 2004
    Location
    Huddinge, Sweden
    Posts
    283
    Probably because you create a <event> element, but selectSingleNode on "/events"

    Rune
    If you hit a brick wall, you didn't jump high enough!

Similar Threads

  1. how to clone database using mysql
    By zarien in forum Database
    Replies: 1
    Last Post: 05-17-2006, 04:27 AM
  2. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 10:09 AM
  3. Replies: 0
    Last Post: 01-24-2006, 02:14 PM
  4. Validating XML
    By Jaco de Villiers in forum XML
    Replies: 1
    Last Post: 06-01-2001, 06:50 PM
  5. Replies: 5
    Last Post: 03-22-2000, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center