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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global file & subs ... 2

Status
Not open for further replies.

kizmar2

Programmer
May 25, 2004
164
US
Shouldn't I be able to add a sub to the "Global.asax.vb" file and use it in a aspx.vb file? I don't have to inherit the global file in the aspx.vb file do I?

KizMar
------------
 
The global.asax file is generally used for application and session events. If you want to create a function that can be used by the whole project, create a class and make a public function for it.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
What exactly do you mean by "create a class"? Do I just create a .VB file that I put "public" functions in?? Will they automatically show, or do I have to import them on every page?

KizMar
------------
 
Create a class as you said and functions within it.

Then you declare a refernce to your class on each page at page level

Dim cls as New myClass
 
OK... what am I doing wrong here...

Code:
Request is not available in this context

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Request is not available in this context

Source Error: 


Line 9:          Dim dsn As String
Line 10:         Dim prodIP = ConfigurationSettings.AppSettings("ProdIP")
Line 11:         Dim server = Request.ServerVariables("SERVER_NAME")
Line 12: 
Line 13:         If server <> prodIP Then
 

Source File: d:\inetpub\[URL unfurl="true"]wwwroot\maximizetech\time\PubClasses.vb[/URL]    Line: 11

I can't even begin to know how to debug using these errors yet. They might as well be in some other language. I'm such a n00b.

Here's PubClasses.vb:

Code:
Imports System.Web
Imports System.Web.SessionState
Imports System.Data.SqlClient

Public Class PubClasses
    Inherits System.Web.HttpApplication

    Public Function OpenConn2DB()
        Dim dsn As String
        Dim prodIP = ConfigurationSettings.AppSettings("ProdIP")
        [COLOR=red]Dim server = Request.ServerVariables("SERVER_NAME")[/color]

        If server <> prodIP Then
            dsn = ConfigurationSettings.AppSettings("DB_Test")
        Else
            dsn = ConfigurationSettings.AppSettings("DB_Live")
        End If

        Dim myConn = New SqlConnection(dsn)

        myConn.Open()
    End Function

End Class

KizMar
------------
 
Within a class you need this syntax"
Code:
Dim server = HttpContext.Current.Server.MachineName
 
Also, add Try/Catch statements to your code as then you can look at the exception that was thrown when you get an error (as it will show you the error message) e.g.
Code:
Try
   ' Code Here
Catch ex As Exception
   ' You can look at the ex object if an error occurs
Finally
   ' If you need to do anything regardless of whether an error occurred
End Try



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hey I think that worked! Thanks!

And thanks for the debugging tip as well. I totally forgot about the try/catch. :)

KizMar
------------
 
OK I have another question... based off the same code above (took out myConn.Open()):

I have my PubClasses.vb file, and in my "login.aspx.vb" I'm doing this:

Code:
Dim sqlQuery = "query here"

Dim myClasses As New PubClasses
    myClasses.SetUpConnection()

Dim myCommand As New SqlCommand(sqlQuery, [COLOR=red]myConn[/color])
Dim myrdr As SqlDataReader = myCommand.ExecuteReader

It's saying: "Name 'myConn' is not declared."

???

KizMar
------------
 
And is it declared?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yeah, it was actually a name space problem.

Another question though:

Should I be able to do a "Imports <main class>.PubClasses" at the top of my login.aspx.vb page instead of doing "Dim myClasses As New PubClasses"?

KizMar
------------
 
No, you'll still have to create a new instance of it. An imports statement will only save you typing the <main class> bit.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top