I want to connect database through configaration file. I am getting error.
Here is my code:
Configuration file:
My code in c# is:
string connStr = ConfigurationManager.ConnectionStrings["databaseconnectionstring"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
con.open();
Thanks
Darma

VulpesRank #55 · 29.2K reputationPosted Jul 23, 2012, 7:29 AM EDT
Shen Hengbin94 reputationPosted Jul 21, 2012, 4:27 AM EDT
Vilas GiteRank #951 · 2.1K reputationPosted Jul 21, 2012, 5:04 AM EDT
please refer:
http://www.dreamincode.net/forums/topic/111383-how-to-connect-the-sql-database-using-application-config-file/
Thanks
darma tejaRank #3,199 · 449 reputationPosted Jul 21, 2012, 6:43 AM EDT
A field initializer cannot reference the non-static field, method, or property.
i am getting error exactly at this line:
SqlConnection con = new SqlConnection(connStr);
Darma
Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 22, 2012, 5:46 AM EDT
Call it in webpage as
string strConnString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
Thanks
If this post helps you mark it as answer
darma tejaRank #3,199 · 449 reputationPosted Jul 22, 2012, 7:32 AM EDT
Thanks allot.
I am developing a desktop application. I want put database connection string in configuration file.
Could you please tell me where i am getting error:
here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace Databaseconfig
{
public partial class Form1 : Form
{
string strConnString = ConfigurationManager.ConnectionStrings[" databaseconnection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
MessageBox.Show("Connection is established");
}
}
}
******************************
My configuration file is:
providerName="System.Data.SqlClient"/>
Thanks,
Darma
VulpesRank #55 · 29.2K reputationPosted Jul 23, 2012, 1:42 AM EDT
darma tejaRank #3,199 · 449 reputationPosted Jul 23, 2012, 6:10 AM EDT
I tried with your code.
Even i am getting error exactly at con.open();
I really tried hard for 2 days. I can not connect through configuration.
I want to open connection in different methods.
Thats why i want create a connection in the class. So that i can open database connection in different methods.
Thanks allot!!
Darma
darma tejaRank #3,199 · 449 reputationPosted Jul 23, 2012, 10:47 AM EDT
You are 100% right. There is a mistake in my connection string.
I have a doubt in this:
When i put connection string without configuration file, for example: like below code, It is working.
SqlConnection con = new SqlConnection("Data Source=DARMATEJA-PC\\MSSQLSERVER2;Initial Catalog=database1.DB;Integrated Security=True");
The same string when i use it in Configuration file it is giving me error.
Now i removed "\" from the string, than it is working. It means the following connection string i wrote in config file.
Data Source=DARMATEJA-PC\MSSQLSERVER2;Initial Catalog=database1.DB;Integrated Security=True
What happens when i remove one "\"?
Darma
VulpesRank #55 · 29.2K reputationPosted Jul 23, 2012, 11:17 AM EDT
\n - new line
\r - carriage return
\t - tab
\b - backspace
Consequently, when you really need a backslash you have to double it, like you've done when passing a string to you SqlConnection constructor.
This doesn't apply to configuration files which are just XML files. So, if you double the backslash in the configuration file, then it won't work properly.