Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In C# which file is act like Vb.net Module file. For example we can use function in module file anywhere in project. That's like in C# we can use like module file.

I am doing Project in ASP.net,C#. I want to give connection string common to all files.
so where i will give the connection string.

Anybody help please

Thanks in advance'
Posted
Comments
Balakrishnan Dhinakaran 14-Dec-11 4:23am    
Good Question but it is not so clear(reason for my vote-4)

Hi,
you can Add your Connection String in Web.config So that is Accessible in All The Files In Application.....

Refer Following Link........

Encryption of Connection Strings Inside the Web.config in ASP.NET 2.0 [^]
 
Share this answer
 
To give connection string common to all pages then you have to give in webconfig file under
<configuration>
tag

eg:
XML
<connectionStrings>
   <add name="MenuStructureDB" connectionString="Data Source=PRASHANTH\SQLEXPRESS;Initial Catalog= MenuStructure;Integrated Security=True;" />
 </connectionStrings>


then in code behind you can call

eg:

C#
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MenuStructureDB"].ToString());
 
Share this answer
 
v3
Comments
devausha 14-Dec-11 5:03am    
I give this above code. But the Error Occured object reference not set to an instance of an object. this error is in
Sqlconnection cn=new sqlconnection
devausha 14-Dec-11 6:20am    
Thank you Buddy.I got the answer.
Thank you very much.
Balakrishnan Dhinakaran 14-Dec-11 6:33am    
No probs..:)
There isn't one - C# has no concept of global variables or methods (a deliberate omission, C and C++ did).

The best way to do this is to create such methods in a separate Utilities DLL - that way they can be added to more than one project just by adding a reference to the DLL to the project. The code does not need to be changed (or even available) to the new project, which improves maintenance, and can promote reliablity.
In the DLL, create a static class, and add public static methods to it.
 
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