user2715779
user2715779

Reputation: 167

How to write connection string in web.config file and read from it?

I'm trying to write Connection string to Web.config like this:

<connectionStrings>
  <add name="Dbconnection" connectionString="Server=localhost; 
       Database=OnlineShopping ; Integrated Security=True"/>
</connectionStrings >

and read from it like this:

string strcon = 
    ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
SqlConnection DbConnection = new SqlConnection(strcon);

when run the program I get an error because of the null reference. but when I use this code:

SqlConnection DbConnection = new SqlConnection();
DbConnection.ConnectionString = 
    "Server=localhost; Database=OnlineShopping ; Integrated Security=True";

I don't get any error and the program works correctly! What is the problem?

Upvotes: 15

Views: 222949

Answers (8)

Opemipo Alomaja
Opemipo Alomaja

Reputation: 21

Two ways of adding connection string

if you set up an instance name while installing mssql make sure to provide the instance name correctly in your web.config file

  1. open your web.config file

Two ways of adding connection string

if you set up an instance name while installing mssql make sure to provide the instance name correctly in your web.config file
1. open your web.config file

  <connectionStrings>
    <add name ="EmployeeAppDB" 
         connectionString="Data Source=INSTANCE_NAME_HERE; <==========
         Initial Catalog=EmployeeDB;Integrated Security=true" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

2. open your web.config file
if you're using the default insallation of mssql with no instance name

  <connectionStrings>
    <add name ="EmployeeAppDB" connectionString="Data Source=.; <=========
         Initial Catalog=EmployeeDB;Integrated Security=true" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

Upvotes: 0

Raju Paladiya
Raju Paladiya

Reputation: 818

After opening the web.config file in application, add sample db connection in connectionStrings section like this:

<connectionStrings>  
    <add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient" />   
</connectionStrings>  

Declaring connectionStrings in web.config file:

 <add name="dbconnection" connectionString="Data Source=Soumalya;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System.Data.SqlClient" />   

There is no need of username and password to access the database server. Now, write the code to get the connection string from web.config file in our codebehind file. Add the following namespace in codebehind file.

using System.Configuration;

This namespace is used to get configuration section details from web.config file.

using System;  
using System.Data.SqlClient;  
using System.Configuration;  
public partial class _Default: System.Web.UI.Page {  
    protected void Page_Load(object sender, EventArgs e) {  
        //Get connection string from web.config file  
        string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;  
        //create new sqlconnection and connection to database by using connection string from web.config file  
        SqlConnection con = new SqlConnection(strcon);  
        con.Open();  
    }  
}  

Upvotes: 0

slfan
slfan

Reputation: 9129

Are you sure that your configuration file (web.config) is at the right place and the connection string is really in the (generated) file? If you publish your file, the content of web.release.config might be copied.

The configuration and the access to the Connection string looks all right to me. I would always add a providername

<connectionStrings>
  <add name="Dbconnection" 
       connectionString="Server=localhost; Database=OnlineShopping; 
       Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Upvotes: 4

Rahul Tripathi
Rahul Tripathi

Reputation: 172608

Add reference to add System.Configuration:-

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

Also you can change the WebConfig file to include the provider name:-

<connectionStrings>
  <add name="Dbconnection" 
       connectionString="Server=localhost; Database=OnlineShopping;
       Integrated Security=True"; providerName="System.Data.SqlClient" />
</connectionStrings>

Upvotes: 12

Suman Banerjee
Suman Banerjee

Reputation: 1961

Web.config:

<connectionStrings>
    <add name="ConnStringDb" connectionString="Data Source=localhost;
         Initial Catalog=DatabaseName; Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

c# code:

using System.Configuration;
using System.Data

SqlConnection _connection = new SqlConnection(
          ConfigurationManager.ConnectionStrings["ConnStringDb"].ToString());

try
{
    if(_connection.State==ConnectionState.Closed)
        _connection.Open();
}
catch { }

Upvotes: 11

user3899014
user3899014

Reputation: 51

Try this After open web.config file in application and add sample db connection in connectionStrings section like this

<connectionStrings>
<add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient"/>
</connectionStrings >

Upvotes: 5

santosh singh
santosh singh

Reputation: 28672

try this

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();

Upvotes: 4

ProgramFOX
ProgramFOX

Reputation: 6390

Try to use WebConfigurationManager instead of ConfigurationManager

Upvotes: 2

Related Questions