.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

How to use database connection which is used in appsettings(webconfig) in the code behind?

Posted By: sheeba     Posted Date: June 02, 2010    Points:2   Category :C#
Hi Friends

I want to use database connection which is used in appsettings(webconfig) in the code behind?

Author: BangaruBabuPureti             
Posted Date: June 02, 2010     Points: 5   

string strCon=ConfigurationManager.Appsettings["appsettingsKeyValue"].ToString();

BangaruBabu Pureti
Author: Lalij Mer             
Posted Date: June 02, 2010     Points: 5   

Show this code sample

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class ConnString : Page

protected DataGrid dataGrid;

protected void Page_Load(object Sender, EventArgs E)
SqlConnection sqlConn = default(SqlConnection);
SqlCommand sqlCmd = default(SqlCommand);
string strConnection = null;

try {
//Get connection string from Web.Config
strConnection = ConfigurationSettings.AppSettings("ConnectionString");
sqlConn = new SqlConnection(strConnection);
sqlCmd = new SqlCommand("SELECT * FROM Customers WHERE " + "(CompanyName LIKE 'A%') OR (CompanyName LIKE 'B%')", sqlConn);
dataGrid.DataSource = sqlCmd.ExecuteReader();
} catch (Exception ex) {
Response.Write(ex.ToString + "<br>");
} finally {


Please Check my answer if you help....
Thank You...

Author: Syed Shakeer Hussain             
Posted Date: June 02, 2010     Points: 5   

The <appSettings> element of a web.config file is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to perform work. The items inside appSettings are items that need to be configurable depending upon the environment, for instance, any database connection strings will change as you move your application from a testing and staging server into production.

<?xml version="1.0" encoding="utf-8" ?>



<compilation defaultLanguage="c#" debug="true" />



<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />



private void Page_Load(object sender, EventArgs e)


string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];

SqlConnection connection = new SqlConnection(connectionInfo);



Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend