Click here to Skip to main content
15,920,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to make connection in web config and how to call in a web page?
Posted

inside WebConfig write,

XML
<connectionStrings>
  <add name="ABC" providerName="System.Data.SqlClient" connectionString ="Data Source=111.11.11.11;Initial Catalog=DBName;User ID=UserID;Password=pass"/>
</connectionStrings>


write this at code side.

string ConnectionString = WebConfigurationManage.ConnectionStrings["ABC"].ConnectionString;



Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
Mohd Wasif 6-Oct-10 7:22am    
Thank Yo Dear It worked
 
Share this answer
 
Comments
Mohd Wasif 6-Oct-10 6:55am    
I am not getting what is defined under this link
You can use it also
In Web.Config file
<connectionstrings>
    <clear />
    <add name="ConnectionString" connectionstring="Data Source=cql-01\sqlexpress;Initial Catalog=eden_outdoor;Integrated Security=True">
      providerName="System.Data.SqlClient">
  </add>
</connectionstrings>

C# code for access
string CS = WebConfigurationManager.ConnectionStrings[0].ConnectionString;
 
Share this answer
 
v4
In the web.config, write this...


XML
<appSettings>
  <add key="connect" value="Server=Server_Name; Integrated Security=true; Database=Database_Name"/>
</appSettings>




In the .cs file, write this...


public SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
AppSettingsReader appobj = new AppSettingsReader();
string str = appobj.GetValue("connect", typeof(String)).ToString();
con = new SqlConnection(str);
con.Open();



I think it might help you....
 
Share this answer
 
v2
<connectionstring>
XML
<add name="DBSource" connectionString="Database=perfact;Server=Amrit;UID=sa;PWD=amrit@123" providerName="System.Data.SqlClient"/>



write the above code in web.config file

add the Code In code behined file e.g con1.aspx

using System.text;
using System.Configuration;
using Mycon;

protect void page_load(object sender, eventArgs e)
{
// to know that connection name DBSource exits or not
lblcon.text=configurationManager.connectionstring["DBSource"].connectionstring;
}

// Make a class in app_code folder

Name it myConnection
and
make a namespace
write the folling code

using System.data;
using System.text;
using System.Configuration.

namespace Mycon // Add this namespace in codebehind file con1.aspx.cs
{
public partial myConnection: classname
{
//Constrotor logic
}
// Make a connection string

public static string connectionstring=configurationManager.connectionstring["DBSource"].connectionstring.
}
//save the file never forgert to add this namespace Mycon in con1.aspx.cs code behind file
 
Share this answer
 
v2
Comments
Ankur\m/ 19-Oct-10 8:03am    
What's the point adding answer to a question which is so old and has already been answered and accepted?
In Web.Config file

XML
<connectionStrings>
  <add name="Connectionstring" providerName="System.Data.SqlClient" connectionString ="Data Source=111.11.11.11;Initial Catalog=DBName;User ID=UserID;Password=pass"/>
</connectionStrings>


and in the .cs file
using System.ConfigurationManager;

string str=ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900