Can't set or get connectionString in c# [duplicate] - c#

This question already has answers here:
.net 3.5: To read connectionstring from app.config?
(4 answers)
Closed 6 years ago.
How to get connection string from below config? I researched on google and also stackoverflow but can't get connection string. And I did not find configuration manager class also although I included System.Configuration.
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections></configSections>
<connectionStrings>
<add name="std_8_science.Properties.Settings.science8ConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=F:\science_8.mdb"
providerName=".NET Framework Data Provider for OLE DB"/>
</connectionStrings>
</configuration>

From comments I see you are trying to read the connection string as a Setting, but it isn't.
Add a project reference to System.Configuration. Then get the string with:
var connectionString = ConfigurationManager.ConnectionStrings["std_8_science.Properties.Settings.science8ConnectionString"].ConnectionString;
Consider using a lot shorter name for your connectionstring. After all it is not a setting...

Your string is off a bit. The below connectino string should get you back up and running
<add name="" connectionString="Data Source=.\SQLEXPRESS;Database=;Initial Catalog=;integrated security=True;" providerName="System.Data.SqlClient"/>
Then, just set the providerName to Microsoft.Jet.OLEDB.4.0
And, clearly, set Database and Initial Catalog
A really good resource is ConnectionStrings.com

Related

Connection String null reference exception in other systems

I need to set .dbml connection string because it's change itself after deploying.
I test many posts in google but no one works for me.
here that links I already test in my app.
1. Setting Connection string in dbml files using Linq to SQL in Visual Studio 2008
2. Point connectionstring in dbml to app.config
in my app code, my connection string is
<add name="PureWater.Properties.Settings.purewaterConnectionS"
connectionString="Data Source=.;Initial Catalog=purewater;Integrated Security=True"
providerName="System.Data.SqlClient" />
According to 1 and 2 abbove, I set .dbml connection none and left empty. Then I modify .dbml designer code.
after that, I call data context (dbml) from other windows forms like this:
pureWaterCxtDataContext cxt = new pureWaterCxtDataContext(ConfigurationManager.ConnectionStrings["PureWater.Properties.Settings.purewaterConnectionS"].ConnectionString);
Everything is ok and I don't have error on my own system but I get an error on other systems on ConfigurationManager.ConnectionStrings that it is a null reference. This is very odd and can't trace exe file in other system to get what's happening?
Thanks in advance for your help
Update:
my app.config file is here:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<configSections>
</configSections>
<connectionStrings>
<add name="PureWater.Properties.Settings.purewaterConnectionS"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=purewaterPartovi; Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Why my connection string is null

i am accessing store proceudre from sql database but it throws at connection string point:
"Object reference not set"
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=HOME-PC;Initial Catalog=LMS;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
i am using windows forms in c#.net and i am newbie.
From one of your comments, it seems perhaps your connection string is defined in the app.config file of a class library. If that's the case, you'll need to copy the connection string entry in the config file to the configuration file of the actual application - in this case, the Windows Forms application that is calling the business layer library.
I found two possible solutions:
A. Removing the providerName="System.Data.SqlClient" part from the connection string definition on the App.config file
or
B. Adding a "using System.Data.SqlClient;" line on the using section of the client code where you are attempting to get and use connectionString, like this:
using System.Data.SqlClient;
//(...other code...)
string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
My guess is that the use of the providerName option on the connectionstring forces a call to the specified provider on execution.

ConfigurationManager does not point to correct app.config

I am trying to use ConfigurationManager to get the connection string from a project call FileShareAccessLibrary.
This is the code I am writting in order to do this:
ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"].ConnectionString
This is the content of app.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections></configSections>
<connectionStrings>
<add name="FileShareAccessLibrary" connectionString="......"
providerName="System.Data.SqlClient" />
<add name="FileShareAccessLibrary.Properties.Settings"
connectionString="..."
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
When I run my app I get a NullReferenceException because ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"] returns null.
While debugging I noticed that none of the two connection strings are stored inside ConfigurationManager.ConnectionStrings so I figured that the ConfigurationManager is pointing to another file.
In my project I have not other app.config file.
Is there something I am doing wrong here?
Why is ConfigurationManager not getting my connection string?
If your FileShareAccessLibrary project is a class library rather than a windows forms application or console application then you will need to move the connection strings (and app settings if you have any) from the FileShareAccessLibrary config file to the config file of the application(s) that reference the FileShareAccessLibrary dll.

connection string issue in web.config of ASP.Net

I am using VSTS 2010 + C# + .Net 4.0 to develop an ASP.Net application using SQL Server 2008 Enterprise as database. I am learning someone else's code. I notice code like this, I am not sure whether the usage is correct and so I come here to ask for advice.
In the code, I see some code like this, I want to know whether using such method to read connection string from web.config is correct?
ConfigurationManager.ConnectionStrings["DBConnectinString"].ConnectionString
and such code is used to read connection string from web.config like below, please notice that connection string is defined outside of system.web section.
<?xml version="1.0"?>
<configuration>
<configSections>
<!--this section is empty-->
</configSections>
<appSettings>
...... content of appSettings
</appSettings>
<connectionStrings>
<add name="DBConnectinString" connectionString="data Source=.;uid=foo;pwd=foo;database=FOODB" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
... ... content of system.web
</system.web>
</configuration>
That is the standard place to define connection strings in web.config, yes.
See this for more info: http://msdn.microsoft.com/en-us/library/ms178411.aspx

Mis-spelling in the .NET configuration system, a design flaw?

I just wrote some .NET code to get connection string from the config file. The config file is as below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="key1" value="hello,world!"/>
</appSettings>
<connectionStrings>
<add name="conn1" connectionString="abcd"/>
</connectionStrings>
</configuration>
.NET Framework provide the following types to get the connection string:
1- ConnectionStringsSection : stands for the config section containing several connection strings
2- ConnectionStringSettingsCollection : stands for the connection string collection
3- ConnectionStringSettings : stands for a certain connection string.
.NET Framework also provide the following types to get the App Settings:
4- AppSettingsSection
5- KeyValueConfigurationCollection
6- KeyValueConfigurationElement
Compare 2 to 5, 3 to 6, why are there extra "s" in ConnectionStringSetting[s]Collection and ConnectionStringSetting[s]?
This mis-spelling is really mis-leading. I think it's a design flaw.
Has anyone noticed that?
Because its in English. Settings implies multiple as does Configuration. A configuration can have multiple properties but a setting is one 'thing'. Configurations would imply a collection of multiple properties. Not a typo just an English language quirk

Categories