Solved

connection string is not read?

Posted on 2014-10-02
6
193 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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