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

8

14

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?

Jaynajayne answered 25/8, 2013 at 16:28 Comment(0)
P
12

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>
Pompano answered 25/8, 2013 at 16:33 Comment(1)
Where to add the reference to the connection string?Daryldaryle
L
11

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 { }
Larrigan answered 10/2, 2014 at 7:5 Comment(0)
E
5

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 >
Eyesore answered 3/9, 2015 at 14:24 Comment(0)
S
4

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>
Sauterne answered 25/8, 2013 at 16:55 Comment(0)
F
3

try this

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();
Featurelength answered 25/8, 2013 at 16:37 Comment(0)
B
2

Try to use WebConfigurationManager instead of ConfigurationManager

Birl answered 25/8, 2013 at 16:32 Comment(1)
you don't need WebConfigurationManager to read a connectionstring.Sauterne
M
0

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();  
    }  
}  
Mistletoe answered 14/7, 2018 at 14:15 Comment(0)
C
0

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>
Clintclintock answered 13/4, 2023 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.