Solved

connection string is not read?

Posted on 2014-10-02
6
184 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 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 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 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

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.

Question has a verified solution.

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

Suggested Solutions

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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

822 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