Solved

connection string is not read?

Posted on 2014-10-02
6
176 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 74

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 33

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 74

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

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 74

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 32

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now