Hi Guys,

I've been working on a little something where details get put into an XML File and then squirted into another form which has been working fine.

I've now added another section to the XML file to hold the Company Name and it gets written to the XML without a problem but when trying to populate the text box on another form with the Company Name value from the XML file it does not work! There is no error either which is annoying.

The Code I'm currently using to try and get the variable is:

        private void Send_Click(object sender, EventArgs e)
        {
            if (File.Exists(@"C:\Timewade\Quickticket\Person.xml"))
            {



                smtpDetails = new XmlDocument();
                smtpDetails.Load(PATH);
                root = smtpDetails.DocumentElement;

                compName.Text = root.GetElementsByTagName("Company_Name")[0].InnerText;

When running the code in debug there is no error. Text box is still blank and does not error when entering,deleting free text from it.

any help would be great!

Many thanks.

Mark.

Hi Mark,

Any chance of getting this XML sheet? Or a stripped down version of it showing the part causing the issue?

Edit: could you also disclose the variable type of root as it cannot be determined from code.

Hi Mikey, good to hear from you..

Yeah, Please find below:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<Table> -<SMTP_details> <SMTP_Server>smtp.********.com</SMTP_Server> <SMTP_Port>25</SMTP_Port> <SMTP_Username>******.****@******.com</SMTP_Username> <SMTP_Password>**********</SMTP_Password> <Company_Name>TEST COMPANY</Company_Name> <Company_Tel>000000000000</Company_Tel> </SMTP_details> </Table>

Cheers, its worth noting standalone="" expects a yes or no as its value.

Can you also disclose the variable type of root as it cannot be determined from code.

        static void Main(string[] args)
        {
            string XMLFilePath = @"C:\Users\maskew\Documents\DaniWeb\TestXML.xml";

            XmlDocument XmlFile = new XmlDocument();
            XmlFile.Load(XMLFilePath);

            XmlNodeList Results = XmlFile.GetElementsByTagName("Company_Name");
            Console.WriteLine(Results[0].InnerText);
            Console.ReadLine();
        }

Little console app I ran and retrieved the value, console displayed: "TEST COMPANY". This method assumes that there is only one entry of the <Company_Name></Company_Name> per XML file.

Is that the case?

Hi Mikey,

Yes it is. Sorry was on the phone... There is only 1 line which has Company Name. Its a very simple app (as i'm still learning).

It bascially records the admin form values into a XML and then should spit it out onto the main form.

In that case you should be able to adapt the above code I gave into your program.

Try:

private void Send_Click(object sender, EventArgs e)
        {
            if (File.Exists(@"C:\Timewade\Quickticket\Person.xml"))
            {
                smtpDetails = new XmlDocument();
                smtpDetails.Load(PATH);

                XmlNodeList results = smtpDetails.GetElementsByTagName("Company_Name");
                compName.Text = results[0].InnerText;

Explanation:

The XmlNodeList will contain all nodes returned from the .GetElementsByTagName() call and from there, as we know there is only one element, we can reference the position 0 in the XmlNodeList and then find its .InnerText which contains what is inside the element in the XML.

Hi Mikey,

I've amended this as requested but still does not seem to pull through the Company Name into the text box. Is there anything else I'm missing here?

Umm I don't see why it wouldnt work :/

What does the variable PATH contain i.e. the actual value? As that could be the only reason it isnt working.

Hi Dave,

the PATH value is the following:

public string PATH = @"C:\Timewade\Quickticket\Person.xml";

The stange thing is.. all the other values in regards to SMTP Server, password etc, etc get read without a problem.

Hi Dave,

Im now multiple people muahahaha!

Hmm thats an odd issue. Especially when its working for me.

Might be worth debugging the method and seeing what the XmlNodeList contains at execution time. Should say count = 1 as one of its properties as it does when I run the program.

Hahah, Sorry Mikey/Dave/Fred.. :)

I'll PM you the Whole Original Code.

Hahah, Sorry Mikey/Dave/Fred.. :)

xD

sent...

Ah Hah! Susses it Mikey,

I realised I was trying to call this in only when I pressed the 'Submit' button. As soon as I did this it came through.

Sorry about that.. my eyes were going blurred this late in the day!

Good to hear, sorry left the office as you sent the code.

I know that feeling oh too well though!

Hi Mikey,

Yeah - I managed to suss that out but now have an even stranger issue where the error states 'Object Reference is not set to an instance of an Object' but i'm not too sure what I would of done to create this problem. :S

Put it in a new thread with all the information we'd normally need and maybe that issue can be solved :)

I've run a step-through on it and it seems to error on the following line of code:

SmtpClient client = new SmtpClient(root.GetElementsByTagName("SMTP_Server")[0].InnerText);

The code up to this part is:

        private void Send_Click(object sender, EventArgs e)
        {
            if (File.Exists(@"C:\Timewade\Quickticket\Person.xml"))
            {



                smtpDetails = new XmlDocument();
                smtpDetails.Load(PATH);


                try
                {
                    SmtpClient client = new SmtpClient(root.GetElementsByTagName("SMTP_Server")[0].InnerText);

but as soon as it hits the "SMTP_Server" details it kicked out and runs the exception message code below:

Code:

               catch (Exception ex)
                {
                    MessageBox.Show("Cannot Send Message: " + ex.Message);
                    textBox1.Clear();
                }

Any ideas? :)

Change the code to select the element to the same as how I selected the Company_Name and see if the error still occurs.

Hi Mikey,

I changed it to the follow and still the same... hmmm :S ???

Code:

                try
                {
                    XmlNodeList results3 =smtpDetails.GetElementsByTagName("SMTP_Server");
                    SmtpClient client = new SmtpClient(results3[0].InnerText);

Again using console application, I fail to recreate the error your seeing :/

            string XMLFilePath = @"C:\Users\maskew\Documents\DaniWeb\TestXML.xml";

            XmlDocument XmlFile = new XmlDocument();
            XmlFile.Load(XMLFilePath);

            XmlNodeList Results = XmlFile.GetElementsByTagName("SMTP_Server");
            Console.WriteLine(Results[0].InnerText);

            SmtpClient client = new SmtpClient(Results[0].InnerText);
            Console.ReadLine();

The following code runs cleanly to the end, I would expect it to throw the same exception as you if that line was the cause.

I was using the following XML file when testing this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Table>
  -<SMTP_details>
    <SMTP_Server>smtp.********.com</SMTP_Server>
    <SMTP_Port>25</SMTP_Port>
    <SMTP_Username>******.****@******.com</SMTP_Username>
    <SMTP_Password>**********</SMTP_Password>
    <Company_Name>TEST COMPANY</Company_Name>
    <Company_Tel>000000000000</Company_Tel>
  </SMTP_details>
</Table>

Hi Mikey, it is indeed very odd..

The only thing difference I can see is I have a 'TRY' in my script but that shouldn't cuase the problem. I will step-through it again and see if i get a different result.

Sure,

Might be worth commenting out the try briefly and running to check it is that line that it is failing on.

Ah-hah! Resolved Mikey :)

When running throug the Debug Step-by-step it was not erroring on the same area anymore.. BUT... it was erroring on pulling in username, password etc, etc as it was not doing it the same way as you told me how to.

Amended this and now seems to be working ok fingers crossed as I have a few more alteration to make to it..

Could you suggest any type of version control fo Visual C# 2010?

My company uses TFS or ClearCase when doing development. Personally I like TFS as it integrates wonderfully with VS2010 however I've never used clearcase so cant talk about that.

HI Mikey, Thanks for that.

I have one more question for now if that's ok... it seems to work fine, however them authenticating with the SMTP Server have take a while on the send...

Is there a way that the form can be minimized and continue to send in the background? and once done pop up with the messagebox.show event?

I already have some script that minimises the Form to the taskbar (next to clock) and I'm guessing there is a way to use this?
Code Below...

Code:

       private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                notifyIcon1.Visible = true;
                notifyIcon1.BalloonTipText = "I'm still here!";
                this.Hide();
            }
        }

        private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        {
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            notifyIcon1.Visible = false;
            this.Show();
            this.WindowState = FormWindowState.Normal;

        }

You could use a background worker thread to send the email. This would leave the main form functional.

MSDN Link

Thanks mikey,

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.