Click here to Skip to main content
15,886,024 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
can i write my connectionstring in the web.config file of my asp.net application and use it in the class library where am writing my dataacces methods.. if possible then how let me know
public bool SaveData(StringBuilder sb)
{
string query = sb.ToString();
bool flag = false;
string strConnection;
strConnection = ConfigurationSettings.AppSettings["dbtest"].ToString();
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
try
{

int i = cmd.ExecuteNonQuery();
if (i > 0)
{
flag = true;

}
else
{
flag = false;
}
}
catch (Exception e)
{
string error = e.Message;
}
finally
{
con.Close();
}
return flag;
}
Posted
Comments
Thanks7872 22-Aug-13 8:22am    
Don't write whole question in title. You have been given enough space for it.

 
Share this answer
 
Comments
Ankur\m/ 28-May-15 8:30am    
Didn't see your answer when I started writing mine. This is the way it is done. 5!
Yes you can do it. If your Class Library project is referenced by your ASP.NET application, you can use below code in your Class Library project to get the connection string from Web.Config File.

C#
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringKeyName"]

This is assuming you are storing your connection string inside <connectionStrings> node in your Web.Config file.

Hope that helps!
 
Share this answer
 
Fixed, Writingf the connection string in the web.congfig
 
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