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


Top 5 Contributors of the Month
david stephan

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?

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



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


BangaruBabu Pureti
http://bangarubabupureti.spaces.live.com/
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);
sqlConn.Open();
dataGrid.DataSource = sqlCmd.ExecuteReader();
dataGrid.DataBind();
} catch (Exception ex) {
Response.Write(ex.ToString + "<br>");
} finally {
sqlConn.Close();
}
}

}


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

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

Hi,
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" ?>

<configuration>

<system.web>

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

</system.web>

<appSettings>

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

</appSettings>

</configuration>


private void Page_Load(object sender, EventArgs e)

{

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

SqlConnection connection = new SqlConnection(connectionInfo);

{
connection.Open();

}
}






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
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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