Connection String null reference exception in other systems - c#

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>

Related

Unable to connect to Local SQL database from app.config in windows form application

i am trying to connect my local MySQL database to my windows form application using app.config.
but i am keep on getting the below mentioned Exception.Can anyone help me please.
Exception :
App.config be like :
<configuration>
<connectionstrings>
<!--<add name ="support_KB" connectionstring="server= .\sqlexpress;database=kbase_support;Integrated Security=SSPI" />-->
<add name ="MyConnectionstringname"
providerName="System.Data.SqlClient"
connectionstring="Data Source=.\sqlexpress;Initial Catalog=kbase_support;Integrated Security=SSPI" />
</connectionstrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
The tag name is case sensetive, it should be:
<connectionStrings>
...
</connectionStrings>
Note: System.Data.SqlClient is the provider for Microsoft SQL Server, not MySQL.
You have two end tags </configSections>. Remove the one before <connectionstrings>
And, <connectionstrings> is case sensitive. It should be <connectionStrings>
Two things to note here.
You are talking about MySql, but connection string looks like for MSSql.
When you initialize your SqlConnection object, make sure you are giving the right connection string name MyConnectionstringname

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.

How to change database that with web configuration is created?

How to change database that web configuration is created?
When I use Asp.net Configuration, visual studio create a default data base (ASPNETDB.MDF),
I want use my database for my web site .
How to do this?
Just add this tab in <connectionStrings> of your Web.Config
<connectionStrings>
<add name="YourConnectionStringName"
connectionString="DataSource=yourDataSource;Initial Catalog=YourDataBaseName;User ID=YourID;Password=YourPassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
You should a add database using this line in web.config
replace with aspnetdb connection string
<connectionStrings>
<add name="LoanCalculatorConnectionString" connectionString="Data Source=servername;Initial Catalog=dbname;Persist Security Info=True;User ID=888;Password=8888"
providerName="System.Data.SqlClient" />
</connectionStrings>
To this porpus use this way:
C://windows/Microsoft.net/Framework/v2.0.50727/aspnet_regsql.exe
. is run
Next--> Configuration SQL server for application service --> select server and database --> Next --> Finish

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.

App.config seems to be ignored

I've this Class Library, as a result of a refactor action.
I added an App.config file and added something like this:
<configuration>
<connectionStrings>
<add name="MyDatabase" connectionString="Data Source=server;Initial Catalog=database;User ID=userid;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when I run the application, debugging learns me this is totally ignored. The immediate window tells me:
ConfigurationManager.ConnectionStrings[0]
{data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
base {System.Configuration.ConfigurationElement}: {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
Key: "LocalSqlServer"
Name: "LocalSqlServer"
Properties: {System.Configuration.ConfigurationPropertyCollection}
ProviderName: "System.Data.SqlClient"
I've checked generated config file in the bin directory and its contents are identical to the App.config.
I try to read the App.config using:
ConfigurationManager.ConnectionStrings[Constants.Connections.DevConnection].ConnectionString
Nothing out of the ordinary I'd say, but what is going wrong?
A class library doesn't get its own config; for an app named Foo.exe you need your configuration to be in Foo.exe.config. The exception here is web apps, where web.config is the naming convention.
This connectionString should be in the corresponding app.config of the exe that you are running.
Is the file being copied to output? Secondly the connection string should be accessible from the AssemblyName.Settings.Properties class which is generated from your code.

Categories