Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Application Varaibles correctly.

Status
Not open for further replies.

vituja

Programmer
Oct 1, 2003
30
US
Hi,

I have a nubie question. I want to store in an app variable my connection string.

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

Application("ConnStr") = "Data Source=dev2;uid=sa;pwd=dev2;database=cunet_db"

End Sub

Then in my public class on the page I want to simply call it with:


Public Class CalendarMain


Dim sqlConn As New SqlConnection(Application("ConnStr"))


BUT I KEEP GETTING AN ERROR:

System.NullReferenceException: Object reference not set to an instance of an object.
 
Dim context As System.Web.HttpContext= System.Web.HttpContext.Current

Context.Application("myvar")
Context.Session("myvar")

Context.Request("myvar")

Most people are putting these value in the web.config.
Your class can access this without using context.

dim configSetting as string = ConfigurationSettings.AppSettings("dataConnection").ToString





<appSettings>
<add key="dataConnection" value="Persist Security Info=False;Data Source=localhost;Initial Catalog=NorthWind;UID=sa;PWD=sa" />
</appSettings>

 
Thanks for the reply RTomes.

So it is more common to put the connect string into the web.config?

I am an asp3 programmer. I'm still learning the ropes in ASP.NET.

So in your second example <appSettings>, that goes into the config file?

Then when I need to use it, how do I access the variable dataConnection?
 
You'd use the line

dim configSetting as string = ConfigurationSettings.AppSettings("dataConnection").ToString

that RT posted above.

If you just need the connection string used for one application, and its not going to be accessed thorugh a business layer and only the presentation, then web.config is the suggested place to put it. If you need more than one app to access it though, then the registry might be a better idea.

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top