Solved

connection string is not read?

Posted on 2014-10-02
6
187 Views
Last Modified: 2014-10-02
When I execute the following code, for cnString, I get:

Data Source=.\SQLExpress12;Att;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True

Whereas I am expecting sting like: TestToGetStringFrom XML File as it is embedded in the cnNorthwind.xml file.

Question: Why it is not reading it correctly? Besides, where does the program gets the above string anyway?
 

Here is cnNorthwind:
<?xml version="1.0" encoding="utf-8" ?>
<connection>
  TestToGetStringFrom XML File
  <!-- Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\NORTHWND.mdf;Integrated Security=True;Connect Timeout=30 -->
</connection>

This is the code reading cnNorthwind:
namespace CodeSampleCS
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            XmlTextReader xmlReader = new XmlTextReader("csNorthwind.xml");
            cnString = xmlReader.ReadElementString("connection");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MessageBox.Show("msg 1" + cnString);
            Application.Run(new frmCS01(cnString));
            MessageBox.Show("msg 2" + cnString);
        }

        public static string cnString { get; set; }
    }
}

FYI: This App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="CodeSampleCS.Properties.Settings.NORTHWNDConnectionString"
            connectionString="TestToGetStringFrom App.config File"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 3
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 40357821
May I inquire as to why you're not storing connection strings in the normal <connectionStrings> section of app.config?
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40357832
I already have. But because I am studying c#, I want to be able to do it in various ways. Eventually, I will need to know how to read something from xml file.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 40357852
I don't see a problem with that. Just keep in mind that the expected place for connection strings is in the place I mentioned above. Deviating from that may lead to confusion for those coming behind you (or even yourself 6 months or a year from now!).

Are you certain that code runs? I get an exception when I run against that specific XML.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40357879
I was getting an exception also. After I copied cnNorthwind.xml manually into Debug folder it ran okay.

Do you have the same exception?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40357895
My exception is with the XmlTextReader itself:

Screenshot
Which basically means that you cannot use that method because there is an embedded XML comment within the node.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 350 total points
ID: 40357903
First to answer the reason why you had to copy your xml file.  
1.  If you select the xml file in the solution explorer.Capture.JPG2.  Choose 'Copy Always' in the Copy to Output Directory property.Capture.JPG
As for this question:

Northwind.xml -
<?xml version="1.0" encoding="utf-8" ?>
<connection>
	DataSource=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\NORTHWND.mdf;Integrated Security=True;Connect Timeout=30
</connection>

Open in new window


Program.cs -
using System;
using System.Xml;

namespace NorthwindExample
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine(new XmlTextReader("Northwind.xml").ReadElementString("connection"));
			Console.ReadLine();
		}
	}
}

Open in new window


Produces this output -Capture.JPG
-saige-
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question