About, how to reduce number of connectionstring,
if you are using only one database as the datasource for your application then you may rename the name of connectionstring in all your databound controls like SQLDataSource, or if you are manually calling some Update, Delete or Insert function, then that also
would need changes in connectionstring. And after renaming all the connectionstring to a common name then you may add a ConnectionString in the WebConfig with the same name.
using System;
using System.Data.SqlClient;
public class MyConnectionFactory
{
public static SqlConnection GetConnection(string name)
{
return new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[name].ConnectionString);
}
}
so everywhere you say new SqlConnection... you'd say MyConnectionFactory.GetConnection("Foo")
If you really want to make your life easier have the factory return a helper class which makes doing db calls easer instead of using a connection directly.
If the answer I provided is useful or informative please check the "answer" button.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
Marked as answer by Figo Fei - MSFT on Feb 13, 2009 07:20 AM
coolx
Member
33 Points
106 Posts
Sql Connection string from web.config?
Feb 08, 2009 04:15 PM|LINK
I wrote sql connection string in the web.cong file like below;
---------------------------
<connectionStrings>
<add name="conn" connectionString="Server=coolxpc\\SQLEXPRESS;Datab ase=db_deneme;uid=sa;pwd=12345" />
</connectionStrings>
----------------------
but when i use this string in default.aspx page, im getting error. (CS0103: the name 'connString' isnt in the content ) This line is below;
-----------------------
conn = System.Configuration.ConfigurationSettings.AppSett ings.Get("baglantim");
conn.Open();
-------------------
There is 7 connection strings in one page and i want to reduce it. How can i do that?
alok.arora
Contributor
3058 Points
530 Posts
Re: Sql Connection string from web.config?
Feb 08, 2009 06:25 PM|LINK
Hi,
About the error that you are getting, you are using the appSettings to get the connectionstring, instead use this:
[VB]
"ConnectionStringName").ConnectionStringSystem.Configuration.ConfigurationManager.ConnectionStrings(
[C#]
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString
About, how to reduce number of connectionstring,
if you are using only one database as the datasource for your application then you may rename the name of connectionstring in all your databound controls like SQLDataSource, or if you are manually calling some Update, Delete or Insert function, then that also would need changes in connectionstring. And after renaming all the connectionstring to a common name then you may add a ConnectionString in the WebConfig with the same name.
vijjendra
Participant
1753 Points
370 Posts
Re: Sql Connection string from web.config?
Feb 08, 2009 06:31 PM|LINK
when u set the connection string in the web.config after that you retrive that values on the page on the following ways,
first include the Namespace using System.Data.SqlClient;
after that use the given code.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
when you define the connectionstring in <connectionStrings></connectionStrings> tag then must use
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
if you define connection string in appsetting then use following code:
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"].ToString());
or use the discussion on the following link:
http://forums.asp.net/t/1381938.aspx
Vijendra Singh
sharmoon
Member
355 Points
65 Posts
Re: Sql Connection string from web.config?
Feb 09, 2009 07:29 AM|LINK
In web.config appSettings section, place your connection string
<appSettings>
<add key="connString" value="yourConnectionString" />
</appSettings>
To access it in your code, use
ConfigurationSettings.AppSettings("connString")
Please "Mark as Answer" if I trust you.
Bhaarat
Contributor
3403 Points
850 Posts
Re: Sql Connection string from web.config?
Feb 09, 2009 08:42 AM|LINK
<connectionStrings>
<add name="TrainingConnectionString" connectionString="Data Source=SERV002;Initial Catalog=Training;Persist Security Info=True;User ID=Test;Password=Test" providerName="System.Data.SqlClient"/>
</connectionStrings>
in aspx.vb
Dim str As String = ConfigurationManager.ConnectionStrings("TrainingConnectionString").ToString()
all The Best
Remember to click "Mark as Answer" on the post that helps U
http://ransandeep.blogspot.com
Rameezwaheed
Contributor
3730 Points
1595 Posts
Re: Sql Connection string from web.config?
Feb 10, 2009 06:02 AM|LINK
Hi Dear,
for reterving Connection String from web.config write as
string connection ;
connection = configurationManager.connectionstring("nameof ur connection").connectionstrings;
sqlconnection conn = new sqlconnection (connection);
conn.open ();
// here ur code
Best Regards
Mark as an answer if it helps
ravi2k7@hotm...
Participant
1315 Points
312 Posts
Re: Sql Connection string from web.config?
Feb 10, 2009 10:37 AM|LINK
HI
Pls just confirm me 'connString' where ur using this value in you code
and you have created the connectionstring in <connectionstrings> layer
but in your code u wrote
conn = System.Configuration.ConfigurationSettings.AppSett ings.Get("baglantim");
so, how it will identify with out creating ur connectionstring in <connectionstring>class
if ur want to access that string when it is in connectionstring class
use like this
conn=System.Configuration.ConfigurationManager.Connectionstring["conn"].Tostriing();
Ravi
Please click "mark as answer" if this post helped you.
coolx
Member
33 Points
106 Posts
Re: Sql Connection string from web.config?
Feb 10, 2009 11:34 AM|LINK
Thanks for your helps guys but its still not working and i really dont know where i am wrong. My all codes below;
Web.config
-----------
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="conn" connectionString="Server=coolxpc\\SQLEXPRESS;Database=db_deneme;uid=sa;pwd=12345" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true">
</compilation>
</system.web>
</configuration>
------------
default.aspx
-----------------------------------------------------------------------------------
<%@ import Namespace="System.Data.SqlClient" %>
<%@ Page Language="c#" codePage="28599" %>
<Script runat="server" language="C#">
void Page_Load(Object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlCommand cmd;
cmd = new SqlCommand("Select * FROM kullanici", connection);
connection.Open();
SqlDataReader alinan_veri3;
alinan_veri3 = cmd.ExecuteReader();
while (alinan_veri3.Read())
{
Response.Write(alinan_veri3["kullanici_ad"]);
}
connection.Close();
}
</Script>
------------------------------------------------------------------------------
I am getting error on bold line, open the connecting line. The error is ;"System.InvalidOperationException: "~sample,instance" error"
mr.debobrata...
Member
384 Points
74 Posts
Re: Sql Connection string from web.config?
Feb 11, 2009 01:29 PM|LINK
Step 1: Change your configuration section by the following
<connectionStrings>
<add name="conn" connectionString="Data Source=coolxpc\\SQLEXPRESS;Initial Catalog=db_deneme;uid=sa;pwd=12345";Integrated Security=False;MultipleActiveResultSets=False;Packet Size=4096;Application Name="Microsoft SQL Server Management Studio Express"" providerName="System.Data.SqlClient"/>
</connectionStrings>
Step 2:
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
You will try the above followings steps , if it is help for you then please click "mark as answer"
If I answered your question please "Mark as Answer"
JeffreyABeck...
All-Star
16423 Points
3329 Posts
Re: Sql Connection string from web.config?
Feb 11, 2009 03:32 PM|LINK
By using a factory. Here's a quick one:
using System; using System.Data.SqlClient; public class MyConnectionFactory { public static SqlConnection GetConnection(string name) { return new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[name].ConnectionString); } }so everywhere you say new SqlConnection... you'd say MyConnectionFactory.GetConnection("Foo")
If you really want to make your life easier have the factory return a helper class which makes doing db calls easer instead of using a connection directly.
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.