connection string in web.config file [Resolved]

Posted by Sharmagoldi781 under .NET Framework on 11/29/2013 | Points: 10 | Views : 2514 | Status : [Member] | Replies : 6
hello
please provide guidence for using connection string a single time in project in web.config file
and then using that name of connection string instead of writing complete query for every database operation (sql) on all pages.
please provide syntax.hope i am understood.




Responses

Posted by: Allemahesh on: 11/29/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Just see the below code so that it will solve your problem.

If Web.Confi file, define a conntionString and give a name to it. see the below code:-

<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=serverName;Initial Catalog=Northwind;Persist Security Info=True;User ID=userName;Password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>


After that you use the below code to read the connectionString on every page.


using System.Web.Configuration;
public string DbConnection = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;


See the links:-

http://msdn.microsoft.com/en-us/library/ms178411.aspx
http://stackoverflow.com/questions/5642474/setting-up-connection-string-in-asp-net-to-sql-server
http://www.codeproject.com/Articles/301726/Web-config-File-ASP-NET

If this helps you towards the solution, click on MARK IT AS ANSWER

Happy Coding.

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Please clear your points.
Do you want the whole queries in web.config or just a connectionstring.

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharmagoldi781 on: 11/29/2013 [Member] Starter | Points: 25

Up
0
Down
i just want to use connection string to be defined in .config file once only and then the name of that string on pages. you can take it as a macro .i just want to avoid overhead of writing entire string on all pages and also updation is also gonig to be easy .hope i am clear

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
So,you can make a class and in the constructor,you can fetch your connection-string from web.config file.Every time,if you make object of that class,then your connection string will be initialized.

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Make a Base class and inherit it in derived class.

In your base class,in constructor you can initialize connection string parameter.

In Derived class,you can have all the CRUD operations.
Once you call any method from derived class,then your base class constructor will be called and initialized, so you do not have to call the connection string.

Please let me know.

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/29/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
See the below link:-

http://www.aspdotnet-suresh.com/2011/11/write-connection-strings-in-webconfig.html

If this helps you towards the solution, click on MARK IT AS ANSWER

Happy Coding.

Sharmagoldi781, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response