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
VulpesPosted Jul 23, 2012, 11:29 AM
VulpesPosted Jul 23, 2012, 3:17 PM
\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.
darma tejaPosted Jul 23, 2012, 2:47 PM
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
darma tejaPosted Jul 23, 2012, 10:10 AM
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
VulpesPosted Jul 23, 2012, 5:42 AM
darma tejaPosted Jul 22, 2012, 11:32 AM
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
Satyapriya NayakPosted Jul 22, 2012, 9:46 AM
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 tejaPosted Jul 21, 2012, 10:43 AM
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
Vilas GitePosted Jul 21, 2012, 9:04 AM
please refer:
http://www.dreamincode.net/forums/topic/111383-how-to-connect-the-sql-database-using-application-config-file/
Thanks
Shen HengbinPosted Jul 21, 2012, 8:27 AM