.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 >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Defining ConnectionString in web.config and using in .aspx page.

Posted By:Nikhil Kumar       Posted Date: April 01, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

Defining ConnectionString in web.config and using in .aspx page. Here we are going to read that how can be define the ConnectionString in web.Config file after defining how can be use this connnection string in our asp.net page.
 

Defining ConnectionString in web.config and using in .aspx page.


Here we are going to read that how can be define the ConnectionString in web.Config file after defining how can be use this connnection string in our asp.net page.

When we define ConnetionString in web.config then there is no need to write ConnetionString in every asp.net page where we require SQL DDL,DML etc .

So first of all open you web.config and if there is not then add this in your project and write this code under Tag __

Here name="connetion" you can give anyname here we will call only this name in your code-behind code for connection and rest are connection details which you can how to give.

[Visual C# code]_____

Now in C# code


string dbstring = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;

System.Data.SqlClient.SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(dnstring);

sqlcon.Open ();

if(sqlcon.State.ToString90=="Open")

{

//Perform data operations

}

Now all the works done type this c# in every place where you want sqlconnection |:-)

If this helped you then please leave your precious comment.

Thanks.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

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