hi...
i'm getting a null reference exception unhandled error.....
i have inserted the code snippet and a screenshot...
please tell me where i'm going wrong.....

DataSources[] dataSources;
   int i = 0;
_list = doc.GetElementsByTagName("DataSource");
                foreach (XmlNode tempNode in _list)
                {
                    //MessageBox.Show(tempNode.Attributes["Name"].Value.ToString());
                    //MessageBox.Show(tempNode.InnerXml.ToString());
                    dataSources[i]=new DataSources();
                    dataSources[i].getContent(tempNode);
                    i++;
                }

the class file for datasources...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace SSRS_Companion
{
    class DataSources
    {
        public string name;
        public string ID;
        public string reference;

        
        public void getContent(XmlNode node)
        {
            try
            {
                //MessageBox.Show(node.InnerXml.ToString());
                this.name = new string(node.Attributes["Name"].Value.ToCharArray());
                this.ID = new string(node.ChildNodes[0].Value.ToCharArray());
                this.reference = new string( node.ChildNodes[1].Value.ToCharArray());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Data Sources");
            }
        }
    }
}

and the screenshot

Recommended Answers

All 6 Replies

In line 1 of your first code block you declare a variable of type DataSource array, but you never actually assign any objects to it. So when you try to index the array in line 8 of the same block, there isn't anything there.

You need something like

DataSources[] dataSources = new DataSources[10];

Note that the 10 is just a value I picked out of the air. I don't know how many you are going to need. You might not know either, in which case I'd use a List<>.

thanks a lot momerath..... :)

In line 1 of your first code block you declare a variable of type DataSource array, but you never actually assign any objects to it. So when you try to index the array in line 8 of the same block, there isn't anything there.

You need something like

DataSources[] dataSources = new DataSources[10];

Note that the 10 is just a value I picked out of the air. I don't know how many you are going to need. You might not know either, in which case I'd use a List<>.

momerath....
that solved the earlier problem... but now i'm getting the error in a different place.....
i have attached the screenshot.....
could you please guide me as to where i'm going wrong.....

my bad.....
this was an error in the node i was passing to the function....

my bad....
there was a mistake in the node i was passing to the function...
sorry... :D

string Path = Application.StartupPath + "\\EmailNewsLetter.exe.config";
string username=textBox1.Text;
string password=textBox2.Text;
string port=textBox3.Text;
string from = textBox4.Text;
//string Path = Application.StartupPath + "\\EmailNewsLetter.exe.config";
Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MailSettingsSectionGroup mailsettign = (MailSettingsSectionGroup)c.GetSectionGroup("system.net/mailSettings");
c.AppSettings.Settings["username"].Value = username.ToString();[/B]
c.AppSettings.Settings["password"].Value = textBox1.Text;
c.AppSettings.Settings["port"].Value = textBox3.Text;
c.AppSettings.Settings["From"].Value = textBox4.Text;
c.Save(ConfigurationSaveMode.Modified);
label5.Text = "Save";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.