This is my VB code for global.asax
<%@ Application Language="VB">
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Application("CS") = "server=myServer; user id=myUser; password=MyPaas; database=myData; pooling=true"
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
Application("CS") = ""
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
End Sub
</script>
I call this value in VB like this:
Dim objConn As New SqlConnection(Application("CS"))
How to do this call in C# ASP.NET?
The equivalent C# code would be:
Note the [] around “CS” instead of the ().
Alternatively, you could do it in a using block:
The using block ensures that dispose is called (even if an exception is encountered). For more information on the using statement, see using Statement (C# Reference).
Though I wonder – why are you using C# with a Global.asax file that’s written in VB.NET?