default.aspx's code is below; now its getting error on bold line; the error is
"System.InvalidOperationException: ExecuteReaderit must get an open and usable connection. Connection's situation is:Closed"
----------------
<%@ import Namespace="System.Data.SqlClient" %>
{
string flmid = Request.QueryString["id"];
SqlConnection baglanti1 = new SqlConnection(ConfigurationManager.ConnectionStrings["baglantim"].ConnectionString);
SqlCommand db_komut3;
db_komut3 = new SqlCommand("Select * FROM kullanici", baglanti1);
SqlDataReader alinan_veri3; alinan_veri3 = db_komut3.ExecuteReader();
while (alinan_veri3.Read())
{
Response.Write(alinan_veri3["kullanici_ad"]);
}
}
coolx
Member
33 Points
106 Posts
Sql connection string from web.config?
Feb 08, 2009 04:34 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?SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: Sql connection string from web.config?
Feb 08, 2009 05:34 PM|LINK
You retrieve a connection string like this:
String ConStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
Then, you create a connection object, assign it the string, and open it.
My blog
coolx
Member
33 Points
106 Posts
Re: Sql connection string from web.config?
Feb 08, 2009 05:38 PM|LINK
"Then, you create a connection object, assign it the string, and open it."
Actually i dont know how can i create ?
coolx
Member
33 Points
106 Posts
Re: Sql connection string from web.config?
Feb 08, 2009 05:49 PM|LINK
the all code is below; its getting error on bold line. If im trying the same connection string with
----------------
void Page_Load(object sender, System.EventArgs e)
{
string baglanti1 = ConfigurationManager.ConnectionStrings["baglantim"];
SqlCommand db_komut3;
db_komut3 = new SqlCommand("Select * FROM kullanici", baglanti1);
SqlDataReader alinan_veri3;
alinan_veri3 = db_komut3.ExecuteReader();
while (alinan_veri3.Read())
{
Response.Write(alinan_veri3["kullanici_ad"]);
}
------------------
If im trying the same connection string wlike below, there is no error and getting datas from db properly
-------------
SqlConnection db_baglanti = new SqlConnection("Server=coolxpc\\SQLEXPRESS;Database=db_deneme;uid=sa;pwd=12345");
----------
SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: Sql connection string from web.config?
Feb 08, 2009 05:49 PM|LINK
Search the online help for SqlConnection class.
Click the Example link.
My blog
coolx
Member
33 Points
106 Posts
Re: Sql connection string from web.config?
Feb 08, 2009 06:02 PM|LINK
i create the one but, its getting error on bold line.
---------------------------------
string baglanti1 = ConfigurationManager.ConnectionStrings["baglantim"]
SqlConnection baglanti1 = new SqlConnection(baglanti1);
//tried like this too SqlConnection baglanti1 = new SqlConnection();
vijjendra
Participant
1753 Points
370 Posts
Re: Sql connection string from web.config?
Feb 08, 2009 06:09 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());
Vijendra Singh
vijjendra
Participant
1753 Points
370 Posts
Re: Sql connection string from web.config?
Feb 08, 2009 06:18 PM|LINK
what error you got on the bold line....
plz post the error
Vijendra Singh
coolx
Member
33 Points
106 Posts
Re: Sql connection string from web.config?
Feb 08, 2009 06:21 PM|LINK
The error is; CS0103: the name 'baglanti1' isnt in the content
coolx
Member
33 Points
106 Posts
Re: Sql connection string from web.config?
Feb 08, 2009 06:25 PM|LINK
default.aspx's code is below; now its getting error on bold line; the error is "System.InvalidOperationException: ExecuteReaderit must get an open and usable connection. Connection's situation is:Closed"
----------------
<%@ import Namespace="System.Data.SqlClient" %>
{
string flmid = Request.QueryString["id"];
SqlConnection baglanti1 = new SqlConnection(ConfigurationManager.ConnectionStrings["baglantim"].ConnectionString);
SqlCommand db_komut3;
db_komut3 = new SqlCommand("Select * FROM kullanici", baglanti1);
SqlDataReader alinan_veri3;
alinan_veri3 = db_komut3.ExecuteReader();
while (alinan_veri3.Read())
{
Response.Write(alinan_veri3["kullanici_ad"]);
}
}
----------------
and the web.config's code;
------------
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="baglantim" 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>
--------