Get connection string from App.config
var connection = ConnectionFactory.GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
And this is my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when my project runs this is my error:
Object reference not set to an instance of an object.
c# ado.net exception-handling connection-string app-config
add a comment |
var connection = ConnectionFactory.GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
And this is my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when my project runs this is my error:
Object reference not set to an instance of an object.
c# ado.net exception-handling connection-string app-config
5
Where did you put that App.config? Project of application you're running or maybe some dll? You need first
– abatishchev
Jun 30 '11 at 15:02
3
Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager.
– daszarrin
Jul 22 '13 at 10:27
Your connection string has a typo. You need a space between "Integrated" and "Security"
– Onur Omer
Aug 23 '16 at 18:34
@OnurOmer - question has been updated to include the space ("Integrated Security" instead of "IntegratedSecurity")
– SlimsGhost
Mar 14 '17 at 15:39
add a comment |
var connection = ConnectionFactory.GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
And this is my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when my project runs this is my error:
Object reference not set to an instance of an object.
c# ado.net exception-handling connection-string app-config
var connection = ConnectionFactory.GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
And this is my App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
But when my project runs this is my error:
Object reference not set to an instance of an object.
c# ado.net exception-handling connection-string app-config
c# ado.net exception-handling connection-string app-config
edited Mar 14 '17 at 15:37
![](https://i.stack.imgur.com/Z3hoz.png?s=32&g=1)
![](https://i.stack.imgur.com/Z3hoz.png?s=32&g=1)
SlimsGhost
2,6141515
2,6141515
asked Jun 30 '11 at 14:54
Moham ad Jafari
1,6962103
1,6962103
5
Where did you put that App.config? Project of application you're running or maybe some dll? You need first
– abatishchev
Jun 30 '11 at 15:02
3
Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager.
– daszarrin
Jul 22 '13 at 10:27
Your connection string has a typo. You need a space between "Integrated" and "Security"
– Onur Omer
Aug 23 '16 at 18:34
@OnurOmer - question has been updated to include the space ("Integrated Security" instead of "IntegratedSecurity")
– SlimsGhost
Mar 14 '17 at 15:39
add a comment |
5
Where did you put that App.config? Project of application you're running or maybe some dll? You need first
– abatishchev
Jun 30 '11 at 15:02
3
Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager.
– daszarrin
Jul 22 '13 at 10:27
Your connection string has a typo. You need a space between "Integrated" and "Security"
– Onur Omer
Aug 23 '16 at 18:34
@OnurOmer - question has been updated to include the space ("Integrated Security" instead of "IntegratedSecurity")
– SlimsGhost
Mar 14 '17 at 15:39
5
5
Where did you put that App.config? Project of application you're running or maybe some dll? You need first
– abatishchev
Jun 30 '11 at 15:02
Where did you put that App.config? Project of application you're running or maybe some dll? You need first
– abatishchev
Jun 30 '11 at 15:02
3
3
Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager.
– daszarrin
Jul 22 '13 at 10:27
Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager.
– daszarrin
Jul 22 '13 at 10:27
Your connection string has a typo. You need a space between "Integrated" and "Security"
– Onur Omer
Aug 23 '16 at 18:34
Your connection string has a typo. You need a space between "Integrated" and "Security"
– Onur Omer
Aug 23 '16 at 18:34
@OnurOmer - question has been updated to include the space ("Integrated Security" instead of "IntegratedSecurity")
– SlimsGhost
Mar 14 '17 at 15:39
@OnurOmer - question has been updated to include the space ("Integrated Security" instead of "IntegratedSecurity")
– SlimsGhost
Mar 14 '17 at 15:39
add a comment |
18 Answers
18
active
oldest
votes
Can't you just do the following:
var connection =
System.Configuration.ConfigurationManager.
ConnectionStrings["Test"].ConnectionString;
Your assembly also needs a reference to System.Configuration.dll
I was not setting the reference due to which getting error.
– Muhammad Danish
Oct 4 '13 at 8:35
7
I know that this is a C# question, but since this is the top of the google search results, to do this in VB, it'sSystem.Configuration.ConfigurationManager.ConnectionStrings("Test").ConnectionString
for those of us who have to maintain VB code
– JFA
Feb 14 '17 at 16:43
add a comment |
Since this is very common question I have prepared some screen shots from Visual Studio to make it easy to follow in 4 simple steps.
20
Love this answer. By default in my version of VS (VS2012 Ultimate) this library is not included, butusing System.Configuration
still works
– David Colwell
Jul 12 '13 at 4:04
1
Could do with stage 4 in the answer. I had typing up stuff..
– PriceCheaperton
Jan 1 '14 at 18:29
2
I really HAD TO add the reference or the using was not underlined but still ConfigurationManager unknown. Thanks!
– CodingYourLife
Dec 13 '17 at 15:09
add a comment |
string str = Properties.Settings.Default.myConnectionString;
1
hmm.. something new to learn.. thanks
– saun4frsh
Jul 26 '14 at 16:58
1
What if app.config is added as a link to a project ?
– FrenkyB
Apr 8 '15 at 19:50
1
This will only work if your connection string is an app setting, it will not work if to retrieve<connectionStrings>
elements from app.config (which is what the OP is asking for!).
– BrainSlugs83
May 2 at 19:52
add a comment |
Also check that you've included the System.Configuration
dll under your references. Without it, you won't have access to the ConfigurationManager
class in the System.Configuration namespace.
add a comment |
First Add a reference of System.Configuration
to your page.
using System.Configuration;
Then According to your app.config get the connection string as follow.
string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();
That's it now you have your connection string in your hand and you can use it.
add a comment |
//Get Connection from web.config file
public static OdbcConnection getConnection()
{
OdbcConnection con = new OdbcConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
return con;
}
add a comment |
Try this out
string abc = ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
add a comment |
1) Create a new form and add this:
Imports System.Configuration
Imports Operaciones.My.MySettings
Public NotInheritable Class frmconexion
Private Shared _cnx As String
Public Shared Property ConexionMySQL() As String
Get
Return My.MySettings.Default.conexionbd
End Get
Private Set(ByVal value As String)
_cnx = value
End Set
End Property
End Class
then when you want to use the connection do this in ur form:
Private Sub frmInsert_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cn As New MySqlConnection(frmconexion.ConexionMySQL)
cn.open()
and thats it. You will be connected to the DB and can do stuff.
This is for vb.net but the logic is the same.
add a comment |
Have you tried:
var connection = new ConnectionFactory().GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
add a comment |
I had the same Issue. my solution was built up from two projects. A Class library
and a website referencing to the class library project. the problem was that i was trying to access the App.config
in my Class library
project but the system was searching in Web.config
of the website. I put the connection string inside Web.config
and ... problem solved!
The main reason was that despite ConfigurationManager
was used in another assembly it was searching inside the runnig project .
add a comment |
string sTemp = System.Configuration.ConfigurationManager.ConnectionStrings["myDB In app.config"].ConnectionString;
add a comment |
You can fetch the connection string by using below line of code -
using System; using System.Configuration;
var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Here is a reference :
Connection String from App.config
1
I did the same as u said but same null reference error I am getting.
– Enigmatic Mind
Sep 13 '16 at 18:41
add a comment |
It seems like problem is not with reference, you are getting connectionstring as null so please make sure you have added the value to the config file your running project meaning the main program/library that gets started/executed first.
add a comment |
It is possible that the OP in this question is trying to use an App.Config within a dll.
In this case, the code is actually trying to access the App.Config of the executable and not the dll. Since the name is not found, you get a Null returned, hence the exception shown.
The following post may be helpful:
ConnectionString from app.config of a DLL is null
my case exactly, thanks
– user1451111
Nov 2 '17 at 9:30
add a comment |
First you have to add System.Configuration
reference to your project and then use below code to get connection string.
_connectionString = ConfigurationManager.ConnectionStrings["MYSQLConnection"].ConnectionString.ToString();
add a comment |
This worked for me:
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
Outputs:
Data Source=.;Initial Catalog=OmidPayamak;IntegratedSecurity=True"
add a comment |
I referenced System.Configuration
library and I have the same error.
The debug files had not their app.config, create manually this file.
The error is, I solved this copying the file "appname.exe.config" in debug folder. The IDE was not create the file.
add a comment |
I solved the problem by using the index to read the string and checking one by one. Reading using the name still gives the same error.
I have the problem when I develop a C# window application, I did not have the problem in my asp.net application. There must be something in the setting which is not right.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6536715%2fget-connection-string-from-app-config%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
Can't you just do the following:
var connection =
System.Configuration.ConfigurationManager.
ConnectionStrings["Test"].ConnectionString;
Your assembly also needs a reference to System.Configuration.dll
I was not setting the reference due to which getting error.
– Muhammad Danish
Oct 4 '13 at 8:35
7
I know that this is a C# question, but since this is the top of the google search results, to do this in VB, it'sSystem.Configuration.ConfigurationManager.ConnectionStrings("Test").ConnectionString
for those of us who have to maintain VB code
– JFA
Feb 14 '17 at 16:43
add a comment |
Can't you just do the following:
var connection =
System.Configuration.ConfigurationManager.
ConnectionStrings["Test"].ConnectionString;
Your assembly also needs a reference to System.Configuration.dll
I was not setting the reference due to which getting error.
– Muhammad Danish
Oct 4 '13 at 8:35
7
I know that this is a C# question, but since this is the top of the google search results, to do this in VB, it'sSystem.Configuration.ConfigurationManager.ConnectionStrings("Test").ConnectionString
for those of us who have to maintain VB code
– JFA
Feb 14 '17 at 16:43
add a comment |
Can't you just do the following:
var connection =
System.Configuration.ConfigurationManager.
ConnectionStrings["Test"].ConnectionString;
Your assembly also needs a reference to System.Configuration.dll
Can't you just do the following:
var connection =
System.Configuration.ConfigurationManager.
ConnectionStrings["Test"].ConnectionString;
Your assembly also needs a reference to System.Configuration.dll
edited Mar 8 '17 at 10:22
![](https://i.stack.imgur.com/7bRC5.jpg?s=32&g=1)
![](https://i.stack.imgur.com/7bRC5.jpg?s=32&g=1)
niico
3,693104994
3,693104994
answered Jun 30 '11 at 20:58
Duffp
4,5202914
4,5202914
I was not setting the reference due to which getting error.
– Muhammad Danish
Oct 4 '13 at 8:35
7
I know that this is a C# question, but since this is the top of the google search results, to do this in VB, it'sSystem.Configuration.ConfigurationManager.ConnectionStrings("Test").ConnectionString
for those of us who have to maintain VB code
– JFA
Feb 14 '17 at 16:43
add a comment |
I was not setting the reference due to which getting error.
– Muhammad Danish
Oct 4 '13 at 8:35
7
I know that this is a C# question, but since this is the top of the google search results, to do this in VB, it'sSystem.Configuration.ConfigurationManager.ConnectionStrings("Test").ConnectionString
for those of us who have to maintain VB code
– JFA
Feb 14 '17 at 16:43
I was not setting the reference due to which getting error.
– Muhammad Danish
Oct 4 '13 at 8:35
I was not setting the reference due to which getting error.
– Muhammad Danish
Oct 4 '13 at 8:35
7
7
I know that this is a C# question, but since this is the top of the google search results, to do this in VB, it's
System.Configuration.ConfigurationManager.ConnectionStrings("Test").ConnectionString
for those of us who have to maintain VB code– JFA
Feb 14 '17 at 16:43
I know that this is a C# question, but since this is the top of the google search results, to do this in VB, it's
System.Configuration.ConfigurationManager.ConnectionStrings("Test").ConnectionString
for those of us who have to maintain VB code– JFA
Feb 14 '17 at 16:43
add a comment |
Since this is very common question I have prepared some screen shots from Visual Studio to make it easy to follow in 4 simple steps.
20
Love this answer. By default in my version of VS (VS2012 Ultimate) this library is not included, butusing System.Configuration
still works
– David Colwell
Jul 12 '13 at 4:04
1
Could do with stage 4 in the answer. I had typing up stuff..
– PriceCheaperton
Jan 1 '14 at 18:29
2
I really HAD TO add the reference or the using was not underlined but still ConfigurationManager unknown. Thanks!
– CodingYourLife
Dec 13 '17 at 15:09
add a comment |
Since this is very common question I have prepared some screen shots from Visual Studio to make it easy to follow in 4 simple steps.
20
Love this answer. By default in my version of VS (VS2012 Ultimate) this library is not included, butusing System.Configuration
still works
– David Colwell
Jul 12 '13 at 4:04
1
Could do with stage 4 in the answer. I had typing up stuff..
– PriceCheaperton
Jan 1 '14 at 18:29
2
I really HAD TO add the reference or the using was not underlined but still ConfigurationManager unknown. Thanks!
– CodingYourLife
Dec 13 '17 at 15:09
add a comment |
Since this is very common question I have prepared some screen shots from Visual Studio to make it easy to follow in 4 simple steps.
Since this is very common question I have prepared some screen shots from Visual Studio to make it easy to follow in 4 simple steps.
edited Dec 19 '14 at 6:12
Mehrad
2,66932849
2,66932849
answered Mar 20 '13 at 17:44
![](https://i.stack.imgur.com/wXvX2.png?s=32&g=1)
![](https://i.stack.imgur.com/wXvX2.png?s=32&g=1)
Fredrick Gauss
4,34412040
4,34412040
20
Love this answer. By default in my version of VS (VS2012 Ultimate) this library is not included, butusing System.Configuration
still works
– David Colwell
Jul 12 '13 at 4:04
1
Could do with stage 4 in the answer. I had typing up stuff..
– PriceCheaperton
Jan 1 '14 at 18:29
2
I really HAD TO add the reference or the using was not underlined but still ConfigurationManager unknown. Thanks!
– CodingYourLife
Dec 13 '17 at 15:09
add a comment |
20
Love this answer. By default in my version of VS (VS2012 Ultimate) this library is not included, butusing System.Configuration
still works
– David Colwell
Jul 12 '13 at 4:04
1
Could do with stage 4 in the answer. I had typing up stuff..
– PriceCheaperton
Jan 1 '14 at 18:29
2
I really HAD TO add the reference or the using was not underlined but still ConfigurationManager unknown. Thanks!
– CodingYourLife
Dec 13 '17 at 15:09
20
20
Love this answer. By default in my version of VS (VS2012 Ultimate) this library is not included, but
using System.Configuration
still works– David Colwell
Jul 12 '13 at 4:04
Love this answer. By default in my version of VS (VS2012 Ultimate) this library is not included, but
using System.Configuration
still works– David Colwell
Jul 12 '13 at 4:04
1
1
Could do with stage 4 in the answer. I had typing up stuff..
– PriceCheaperton
Jan 1 '14 at 18:29
Could do with stage 4 in the answer. I had typing up stuff..
– PriceCheaperton
Jan 1 '14 at 18:29
2
2
I really HAD TO add the reference or the using was not underlined but still ConfigurationManager unknown. Thanks!
– CodingYourLife
Dec 13 '17 at 15:09
I really HAD TO add the reference or the using was not underlined but still ConfigurationManager unknown. Thanks!
– CodingYourLife
Dec 13 '17 at 15:09
add a comment |
string str = Properties.Settings.Default.myConnectionString;
1
hmm.. something new to learn.. thanks
– saun4frsh
Jul 26 '14 at 16:58
1
What if app.config is added as a link to a project ?
– FrenkyB
Apr 8 '15 at 19:50
1
This will only work if your connection string is an app setting, it will not work if to retrieve<connectionStrings>
elements from app.config (which is what the OP is asking for!).
– BrainSlugs83
May 2 at 19:52
add a comment |
string str = Properties.Settings.Default.myConnectionString;
1
hmm.. something new to learn.. thanks
– saun4frsh
Jul 26 '14 at 16:58
1
What if app.config is added as a link to a project ?
– FrenkyB
Apr 8 '15 at 19:50
1
This will only work if your connection string is an app setting, it will not work if to retrieve<connectionStrings>
elements from app.config (which is what the OP is asking for!).
– BrainSlugs83
May 2 at 19:52
add a comment |
string str = Properties.Settings.Default.myConnectionString;
string str = Properties.Settings.Default.myConnectionString;
edited Nov 17 '15 at 20:29
Celeo
4,28382535
4,28382535
answered Oct 3 '13 at 11:38
gjijo
1,0931113
1,0931113
1
hmm.. something new to learn.. thanks
– saun4frsh
Jul 26 '14 at 16:58
1
What if app.config is added as a link to a project ?
– FrenkyB
Apr 8 '15 at 19:50
1
This will only work if your connection string is an app setting, it will not work if to retrieve<connectionStrings>
elements from app.config (which is what the OP is asking for!).
– BrainSlugs83
May 2 at 19:52
add a comment |
1
hmm.. something new to learn.. thanks
– saun4frsh
Jul 26 '14 at 16:58
1
What if app.config is added as a link to a project ?
– FrenkyB
Apr 8 '15 at 19:50
1
This will only work if your connection string is an app setting, it will not work if to retrieve<connectionStrings>
elements from app.config (which is what the OP is asking for!).
– BrainSlugs83
May 2 at 19:52
1
1
hmm.. something new to learn.. thanks
– saun4frsh
Jul 26 '14 at 16:58
hmm.. something new to learn.. thanks
– saun4frsh
Jul 26 '14 at 16:58
1
1
What if app.config is added as a link to a project ?
– FrenkyB
Apr 8 '15 at 19:50
What if app.config is added as a link to a project ?
– FrenkyB
Apr 8 '15 at 19:50
1
1
This will only work if your connection string is an app setting, it will not work if to retrieve
<connectionStrings>
elements from app.config (which is what the OP is asking for!).– BrainSlugs83
May 2 at 19:52
This will only work if your connection string is an app setting, it will not work if to retrieve
<connectionStrings>
elements from app.config (which is what the OP is asking for!).– BrainSlugs83
May 2 at 19:52
add a comment |
Also check that you've included the System.Configuration
dll under your references. Without it, you won't have access to the ConfigurationManager
class in the System.Configuration namespace.
add a comment |
Also check that you've included the System.Configuration
dll under your references. Without it, you won't have access to the ConfigurationManager
class in the System.Configuration namespace.
add a comment |
Also check that you've included the System.Configuration
dll under your references. Without it, you won't have access to the ConfigurationManager
class in the System.Configuration namespace.
Also check that you've included the System.Configuration
dll under your references. Without it, you won't have access to the ConfigurationManager
class in the System.Configuration namespace.
answered Jul 19 '12 at 7:49
Carl Heinrich Hancke
1,84012328
1,84012328
add a comment |
add a comment |
First Add a reference of System.Configuration
to your page.
using System.Configuration;
Then According to your app.config get the connection string as follow.
string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();
That's it now you have your connection string in your hand and you can use it.
add a comment |
First Add a reference of System.Configuration
to your page.
using System.Configuration;
Then According to your app.config get the connection string as follow.
string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();
That's it now you have your connection string in your hand and you can use it.
add a comment |
First Add a reference of System.Configuration
to your page.
using System.Configuration;
Then According to your app.config get the connection string as follow.
string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();
That's it now you have your connection string in your hand and you can use it.
First Add a reference of System.Configuration
to your page.
using System.Configuration;
Then According to your app.config get the connection string as follow.
string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();
That's it now you have your connection string in your hand and you can use it.
edited Aug 12 '16 at 17:36
![](https://i.stack.imgur.com/Ub5mH.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Ub5mH.jpg?s=32&g=1)
iliketocode
5,42943046
5,42943046
answered Jan 16 '14 at 6:51
![](https://i.stack.imgur.com/wHZFb.jpg?s=32&g=1)
![](https://i.stack.imgur.com/wHZFb.jpg?s=32&g=1)
Tapan kumar
4,37711719
4,37711719
add a comment |
add a comment |
//Get Connection from web.config file
public static OdbcConnection getConnection()
{
OdbcConnection con = new OdbcConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
return con;
}
add a comment |
//Get Connection from web.config file
public static OdbcConnection getConnection()
{
OdbcConnection con = new OdbcConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
return con;
}
add a comment |
//Get Connection from web.config file
public static OdbcConnection getConnection()
{
OdbcConnection con = new OdbcConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
return con;
}
//Get Connection from web.config file
public static OdbcConnection getConnection()
{
OdbcConnection con = new OdbcConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
return con;
}
edited Aug 29 '17 at 17:08
![](https://i.stack.imgur.com/xT7vO.png?s=32&g=1)
![](https://i.stack.imgur.com/xT7vO.png?s=32&g=1)
Massimiliano Kraus
2,34741532
2,34741532
answered Apr 2 '15 at 7:56
Gobind Mandal
16122
16122
add a comment |
add a comment |
Try this out
string abc = ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
add a comment |
Try this out
string abc = ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
add a comment |
Try this out
string abc = ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
Try this out
string abc = ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
edited Aug 12 '16 at 17:36
![](https://i.stack.imgur.com/Ub5mH.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Ub5mH.jpg?s=32&g=1)
iliketocode
5,42943046
5,42943046
answered Apr 29 '14 at 7:05
![](https://i.stack.imgur.com/p5EPk.jpg?s=32&g=1)
![](https://i.stack.imgur.com/p5EPk.jpg?s=32&g=1)
vishu9219
528512
528512
add a comment |
add a comment |
1) Create a new form and add this:
Imports System.Configuration
Imports Operaciones.My.MySettings
Public NotInheritable Class frmconexion
Private Shared _cnx As String
Public Shared Property ConexionMySQL() As String
Get
Return My.MySettings.Default.conexionbd
End Get
Private Set(ByVal value As String)
_cnx = value
End Set
End Property
End Class
then when you want to use the connection do this in ur form:
Private Sub frmInsert_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cn As New MySqlConnection(frmconexion.ConexionMySQL)
cn.open()
and thats it. You will be connected to the DB and can do stuff.
This is for vb.net but the logic is the same.
add a comment |
1) Create a new form and add this:
Imports System.Configuration
Imports Operaciones.My.MySettings
Public NotInheritable Class frmconexion
Private Shared _cnx As String
Public Shared Property ConexionMySQL() As String
Get
Return My.MySettings.Default.conexionbd
End Get
Private Set(ByVal value As String)
_cnx = value
End Set
End Property
End Class
then when you want to use the connection do this in ur form:
Private Sub frmInsert_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cn As New MySqlConnection(frmconexion.ConexionMySQL)
cn.open()
and thats it. You will be connected to the DB and can do stuff.
This is for vb.net but the logic is the same.
add a comment |
1) Create a new form and add this:
Imports System.Configuration
Imports Operaciones.My.MySettings
Public NotInheritable Class frmconexion
Private Shared _cnx As String
Public Shared Property ConexionMySQL() As String
Get
Return My.MySettings.Default.conexionbd
End Get
Private Set(ByVal value As String)
_cnx = value
End Set
End Property
End Class
then when you want to use the connection do this in ur form:
Private Sub frmInsert_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cn As New MySqlConnection(frmconexion.ConexionMySQL)
cn.open()
and thats it. You will be connected to the DB and can do stuff.
This is for vb.net but the logic is the same.
1) Create a new form and add this:
Imports System.Configuration
Imports Operaciones.My.MySettings
Public NotInheritable Class frmconexion
Private Shared _cnx As String
Public Shared Property ConexionMySQL() As String
Get
Return My.MySettings.Default.conexionbd
End Get
Private Set(ByVal value As String)
_cnx = value
End Set
End Property
End Class
then when you want to use the connection do this in ur form:
Private Sub frmInsert_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cn As New MySqlConnection(frmconexion.ConexionMySQL)
cn.open()
and thats it. You will be connected to the DB and can do stuff.
This is for vb.net but the logic is the same.
edited Aug 12 '16 at 17:37
![](https://i.stack.imgur.com/Ub5mH.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Ub5mH.jpg?s=32&g=1)
iliketocode
5,42943046
5,42943046
answered Nov 28 '12 at 4:13
OREO
311
311
add a comment |
add a comment |
Have you tried:
var connection = new ConnectionFactory().GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
add a comment |
Have you tried:
var connection = new ConnectionFactory().GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
add a comment |
Have you tried:
var connection = new ConnectionFactory().GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
Have you tried:
var connection = new ConnectionFactory().GetConnection(
ConfigurationManager.ConnectionStrings["Test"]
.ConnectionString, DataBaseProvider);
edited Mar 8 '17 at 10:22
![](https://i.stack.imgur.com/7bRC5.jpg?s=32&g=1)
![](https://i.stack.imgur.com/7bRC5.jpg?s=32&g=1)
niico
3,693104994
3,693104994
answered Jun 30 '11 at 15:24
![](https://i.stack.imgur.com/mPvRH.jpg?s=32&g=1)
![](https://i.stack.imgur.com/mPvRH.jpg?s=32&g=1)
Vlad Bezden
27.8k10124111
27.8k10124111
add a comment |
add a comment |
I had the same Issue. my solution was built up from two projects. A Class library
and a website referencing to the class library project. the problem was that i was trying to access the App.config
in my Class library
project but the system was searching in Web.config
of the website. I put the connection string inside Web.config
and ... problem solved!
The main reason was that despite ConfigurationManager
was used in another assembly it was searching inside the runnig project .
add a comment |
I had the same Issue. my solution was built up from two projects. A Class library
and a website referencing to the class library project. the problem was that i was trying to access the App.config
in my Class library
project but the system was searching in Web.config
of the website. I put the connection string inside Web.config
and ... problem solved!
The main reason was that despite ConfigurationManager
was used in another assembly it was searching inside the runnig project .
add a comment |
I had the same Issue. my solution was built up from two projects. A Class library
and a website referencing to the class library project. the problem was that i was trying to access the App.config
in my Class library
project but the system was searching in Web.config
of the website. I put the connection string inside Web.config
and ... problem solved!
The main reason was that despite ConfigurationManager
was used in another assembly it was searching inside the runnig project .
I had the same Issue. my solution was built up from two projects. A Class library
and a website referencing to the class library project. the problem was that i was trying to access the App.config
in my Class library
project but the system was searching in Web.config
of the website. I put the connection string inside Web.config
and ... problem solved!
The main reason was that despite ConfigurationManager
was used in another assembly it was searching inside the runnig project .
answered Mar 29 '17 at 8:51
![](https://i.stack.imgur.com/ypTqj.jpg?s=32&g=1)
![](https://i.stack.imgur.com/ypTqj.jpg?s=32&g=1)
AmiNadimi
1,62211732
1,62211732
add a comment |
add a comment |
string sTemp = System.Configuration.ConfigurationManager.ConnectionStrings["myDB In app.config"].ConnectionString;
add a comment |
string sTemp = System.Configuration.ConfigurationManager.ConnectionStrings["myDB In app.config"].ConnectionString;
add a comment |
string sTemp = System.Configuration.ConfigurationManager.ConnectionStrings["myDB In app.config"].ConnectionString;
string sTemp = System.Configuration.ConfigurationManager.ConnectionStrings["myDB In app.config"].ConnectionString;
edited Dec 8 '12 at 12:22
j0k
20.1k136575
20.1k136575
answered Dec 8 '12 at 12:04
khatami
211
211
add a comment |
add a comment |
You can fetch the connection string by using below line of code -
using System; using System.Configuration;
var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Here is a reference :
Connection String from App.config
1
I did the same as u said but same null reference error I am getting.
– Enigmatic Mind
Sep 13 '16 at 18:41
add a comment |
You can fetch the connection string by using below line of code -
using System; using System.Configuration;
var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Here is a reference :
Connection String from App.config
1
I did the same as u said but same null reference error I am getting.
– Enigmatic Mind
Sep 13 '16 at 18:41
add a comment |
You can fetch the connection string by using below line of code -
using System; using System.Configuration;
var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Here is a reference :
Connection String from App.config
You can fetch the connection string by using below line of code -
using System; using System.Configuration;
var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
Here is a reference :
Connection String from App.config
answered Oct 17 '13 at 8:45
somesh
1,8382129
1,8382129
1
I did the same as u said but same null reference error I am getting.
– Enigmatic Mind
Sep 13 '16 at 18:41
add a comment |
1
I did the same as u said but same null reference error I am getting.
– Enigmatic Mind
Sep 13 '16 at 18:41
1
1
I did the same as u said but same null reference error I am getting.
– Enigmatic Mind
Sep 13 '16 at 18:41
I did the same as u said but same null reference error I am getting.
– Enigmatic Mind
Sep 13 '16 at 18:41
add a comment |
It seems like problem is not with reference, you are getting connectionstring as null so please make sure you have added the value to the config file your running project meaning the main program/library that gets started/executed first.
add a comment |
It seems like problem is not with reference, you are getting connectionstring as null so please make sure you have added the value to the config file your running project meaning the main program/library that gets started/executed first.
add a comment |
It seems like problem is not with reference, you are getting connectionstring as null so please make sure you have added the value to the config file your running project meaning the main program/library that gets started/executed first.
It seems like problem is not with reference, you are getting connectionstring as null so please make sure you have added the value to the config file your running project meaning the main program/library that gets started/executed first.
answered Jan 9 '14 at 6:17
![](https://i.stack.imgur.com/XukIU.jpg?s=32&g=1)
![](https://i.stack.imgur.com/XukIU.jpg?s=32&g=1)
Chandra Malla
2,0591411
2,0591411
add a comment |
add a comment |
It is possible that the OP in this question is trying to use an App.Config within a dll.
In this case, the code is actually trying to access the App.Config of the executable and not the dll. Since the name is not found, you get a Null returned, hence the exception shown.
The following post may be helpful:
ConnectionString from app.config of a DLL is null
my case exactly, thanks
– user1451111
Nov 2 '17 at 9:30
add a comment |
It is possible that the OP in this question is trying to use an App.Config within a dll.
In this case, the code is actually trying to access the App.Config of the executable and not the dll. Since the name is not found, you get a Null returned, hence the exception shown.
The following post may be helpful:
ConnectionString from app.config of a DLL is null
my case exactly, thanks
– user1451111
Nov 2 '17 at 9:30
add a comment |
It is possible that the OP in this question is trying to use an App.Config within a dll.
In this case, the code is actually trying to access the App.Config of the executable and not the dll. Since the name is not found, you get a Null returned, hence the exception shown.
The following post may be helpful:
ConnectionString from app.config of a DLL is null
It is possible that the OP in this question is trying to use an App.Config within a dll.
In this case, the code is actually trying to access the App.Config of the executable and not the dll. Since the name is not found, you get a Null returned, hence the exception shown.
The following post may be helpful:
ConnectionString from app.config of a DLL is null
edited May 23 '17 at 12:26
Community♦
11
11
answered Jun 29 '15 at 21:15
Andrew
439519
439519
my case exactly, thanks
– user1451111
Nov 2 '17 at 9:30
add a comment |
my case exactly, thanks
– user1451111
Nov 2 '17 at 9:30
my case exactly, thanks
– user1451111
Nov 2 '17 at 9:30
my case exactly, thanks
– user1451111
Nov 2 '17 at 9:30
add a comment |
First you have to add System.Configuration
reference to your project and then use below code to get connection string.
_connectionString = ConfigurationManager.ConnectionStrings["MYSQLConnection"].ConnectionString.ToString();
add a comment |
First you have to add System.Configuration
reference to your project and then use below code to get connection string.
_connectionString = ConfigurationManager.ConnectionStrings["MYSQLConnection"].ConnectionString.ToString();
add a comment |
First you have to add System.Configuration
reference to your project and then use below code to get connection string.
_connectionString = ConfigurationManager.ConnectionStrings["MYSQLConnection"].ConnectionString.ToString();
First you have to add System.Configuration
reference to your project and then use below code to get connection string.
_connectionString = ConfigurationManager.ConnectionStrings["MYSQLConnection"].ConnectionString.ToString();
edited Mar 14 '17 at 15:32
Tasos K.
6,57552848
6,57552848
answered Mar 14 '17 at 15:26
SagarJaybhay
113
113
add a comment |
add a comment |
This worked for me:
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
Outputs:
Data Source=.;Initial Catalog=OmidPayamak;IntegratedSecurity=True"
add a comment |
This worked for me:
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
Outputs:
Data Source=.;Initial Catalog=OmidPayamak;IntegratedSecurity=True"
add a comment |
This worked for me:
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
Outputs:
Data Source=.;Initial Catalog=OmidPayamak;IntegratedSecurity=True"
This worked for me:
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
Outputs:
Data Source=.;Initial Catalog=OmidPayamak;IntegratedSecurity=True"
edited Aug 29 '17 at 17:07
![](https://i.stack.imgur.com/xT7vO.png?s=32&g=1)
![](https://i.stack.imgur.com/xT7vO.png?s=32&g=1)
Massimiliano Kraus
2,34741532
2,34741532
answered Feb 13 '17 at 7:45
Talha Imam
191314
191314
add a comment |
add a comment |
I referenced System.Configuration
library and I have the same error.
The debug files had not their app.config, create manually this file.
The error is, I solved this copying the file "appname.exe.config" in debug folder. The IDE was not create the file.
add a comment |
I referenced System.Configuration
library and I have the same error.
The debug files had not their app.config, create manually this file.
The error is, I solved this copying the file "appname.exe.config" in debug folder. The IDE was not create the file.
add a comment |
I referenced System.Configuration
library and I have the same error.
The debug files had not their app.config, create manually this file.
The error is, I solved this copying the file "appname.exe.config" in debug folder. The IDE was not create the file.
I referenced System.Configuration
library and I have the same error.
The debug files had not their app.config, create manually this file.
The error is, I solved this copying the file "appname.exe.config" in debug folder. The IDE was not create the file.
edited Aug 12 '16 at 17:37
![](https://i.stack.imgur.com/Ub5mH.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Ub5mH.jpg?s=32&g=1)
iliketocode
5,42943046
5,42943046
answered Jun 16 '15 at 18:20
jjcaicedo
1
1
add a comment |
add a comment |
I solved the problem by using the index to read the string and checking one by one. Reading using the name still gives the same error.
I have the problem when I develop a C# window application, I did not have the problem in my asp.net application. There must be something in the setting which is not right.
add a comment |
I solved the problem by using the index to read the string and checking one by one. Reading using the name still gives the same error.
I have the problem when I develop a C# window application, I did not have the problem in my asp.net application. There must be something in the setting which is not right.
add a comment |
I solved the problem by using the index to read the string and checking one by one. Reading using the name still gives the same error.
I have the problem when I develop a C# window application, I did not have the problem in my asp.net application. There must be something in the setting which is not right.
I solved the problem by using the index to read the string and checking one by one. Reading using the name still gives the same error.
I have the problem when I develop a C# window application, I did not have the problem in my asp.net application. There must be something in the setting which is not right.
answered Oct 26 '17 at 7:37
dew17
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6536715%2fget-connection-string-from-app-config%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Where did you put that App.config? Project of application you're running or maybe some dll? You need first
– abatishchev
Jun 30 '11 at 15:02
3
Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager.
– daszarrin
Jul 22 '13 at 10:27
Your connection string has a typo. You need a space between "Integrated" and "Security"
– Onur Omer
Aug 23 '16 at 18:34
@OnurOmer - question has been updated to include the space ("Integrated Security" instead of "IntegratedSecurity")
– SlimsGhost
Mar 14 '17 at 15:39