Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

connection string is not read?

Posted on 2014-10-02
6
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 600 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 600 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 34

Accepted Solution

by:
it_saige earned 1400 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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