Mike Eghtebas
asked on
connection string is not read?
When I execute the following code, for cnString, I get:
Data Source=.\SQLExpress12;Att; AttachDbFi lename=|Da taDirector y|\NORTHWN D.MDF;Inte grated 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?
Data Source=.\SQLExpress12;Att;
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was getting an exception also. After I copied cnNorthwind.xml manually into Debug folder it ran okay.
Do you have the same exception?
Do you have the same exception?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER