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.
Related
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>
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
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.
I have created a ASP.NET C# MVC project. I am trying to connect it to a MSSQL DB. In the Web.config file i added the following;
<connectionStrings>
<add name="sdbconnection" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\o\documents\visual studio 2012\Projects\ppl\ppl\App_Data\ppldb.mdf";Integrated Security=True" />
</connectionStrings>
But, the above code is incorrect ; I want it to be as follows;
<connectionStrings>
<add name="sdbconnection" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|ppldb.mdf;Integrated Security=True" />
</connectionStrings>
But What is DataDirectory ? Where do i specify it ?
What is name="sdbconnection" ? Can i give any name that i want ?
First question
Quoting MSDN:
The |DataDirectory| portion of the connection string specifies that
the MDF file is located inthe App_Data directory
Moreover you can change it using following function:
AppDomain.CurrentDomain.SetData("DataDirectory", "D:\database");
Second question
name="sdbconnection" allows you to use specified conection string. So in your code you can write:
string connectionString = ConfigurationManager.ConnectionStrings["sdbconnection"].ConnectionString;
to acces this connection string
Name is use to identify and use code your applications
Please refer this article http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config
Data Directory is usually App_Data folder and in web.config file name is use to use connection string in the application
Ex:String conn=ConfigurationManager.ConnectionStrings["Your name at web.config file"].ConnectionString;
or
String conn=ConfigurationManager.ConnectionStrings["Your name at web.config file"].toString();
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