Solved

Global.asa TO Global.asax

Posted on 2006-11-01
13
514 Views
Last Modified: 2012-06-27
Guys, I'm having a difficult time with something.  I am a newbie to asp.net and I do apologize for the rudimentary question, but I have nowhere else to turn.  I have inherited a web site that uses classic asp and a global.asa file to store some session info.  However, I have been instructed to begin creating a new set of pages to add .NET functionality to the already existing site.  

Here's some code from an already existing global.asa:
<-- begin code -->

<SCRIPT LANGUAGE="VBScript" RUNAT="SERVER">
Sub Session_onStart()
   dim auth_user,x,con,rst,sql,lefthdr
   set con=Server.Createobject("ADODB.Connection")  
   Set rst= Server.CreateObject("ADODB.Recordset")
   auth_user = ucase(request.servervariables("auth_user"))
   x=len(auth_user)
   if left(auth_user,9)="mydomain\" then auth_user=right(auth_user,x-9) end if
   if left(auth_user,4)="myd\" then auth_user=right(auth_user,x-4) end if
   session("authuser")=auth_user  
   sql = "select num,name from people where userid='" & auth_user & "'"
   con.open "Provider=SQLOLEDB; Data Source=1.1.1.1; Initial Catalog=people; User ID=myid; Password=xxxxxxx"
   rst.open sql, con
   if not rst.eof then
      session("name") = rst("name")
      session("num") = rst("num")
        
       else
      session("name") = ""
      session("num") = ""        

        end if
   rst.close
   con.close
end Sub

Sub Session_onEnd()
Session("authuser")= Nothing
Session("name")= Nothing
Session("num")= Nothing
end Sub
</SCRIPT>

<-- End Code -- >

The site uses the session vars "name" and "num" at various places through the site.  Works great for classic asp application.  Now, I've created a new virtual directory to use asp 2.0 code and I wish to create a global.asax file to do the same as the global.asa.  However, I have not found much information on the Internet on how to accomplish this feat.   Would any of you so kind as to how to point me into the right direction?  
0
Comment
Question by:jguy07
  • 7
  • 6
13 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 250 total points
ID: 17853032
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
        Dim auth_user, x, sql, lefthdr As String
        Dim con As SqlClient.SqlConnection
        Dim ds As New DataSet
        Try
            auth_user = UCase(Request.ServerVariables("auth_user"))
            x = Len(auth_user)
            If Left(auth_user, 9) = "mydomain\" Then
                auth_user = Right(auth_user, x - 9)
            End If
            If Left(auth_user, 4) = "myd\" Then
                auth_user = Right(auth_user, x - 4)
            End If
            Session("authuser") = auth_user
            sql = "select num,name from people where userid='" & auth_user & "'"
            con = New SqlClient.SqlConnection("Provider=SQLOLEDB; Data Source=1.1.1.1; Initial Catalog=people; User ID=myid; Password=xxxxxxx")
            con.Open()
            Dim da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sql, con)
            ''make sure command does not timeout
            da.SelectCommand.CommandTimeout = 0
            ''fill dataset
            da.Fill(ds)
            If ds.Tables(0).Rows.Count > 0 Then
                Session("name") = ds.Tables(0).Rows(0).Item("name")
                Session("num") = ds.Tables(0).Rows(0).Item("num")

            Else
                Session("name") = ""
                Session("num") = ""

            End If
        Catch ex As Exception
            Console.Write(ex.Message)
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try
    End Sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17853042
Sub Session_End()
Session("authuser")= Nothing
Session("name")= Nothing
Session("num")= Nothing
end Sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17853043
place the above inside your Global.asax file
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:jguy07
ID: 17853191
YZlat,
Many thanks!  For some reason, I had to comment out these lines:
        Finally
            ' If con.State = ConnectionState.Open Then
            'con.Close()
            'End If
        End Try

VB.NET compiler complained about "con" having a null ref?  
0
 

Author Comment

by:jguy07
ID: 17853500
Also, I am not getting the desired results.  I have a label that displays either the auth_user (if they are not listed in the sql database) OR it lists the session("name") from the ds.Tables(0).Rows(0).Item("name") listed in your code above.  For some reason, the session("name") varible is null/blank if the person's name is in the db.  If the person's name is not located in the db, but they are authenticated, the session("authuser") is successfully displayed on the label's text.  How can I check to assure that the sql connection is actually returning rows?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858279
this line checks if dataset returns any rows:
 If ds.Tables(0).Rows.Count > 0 Then
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858284
also try changing this

 Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try


to

 Finally
 if not con=nothing then
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
end if
        End Try
0
 

Author Comment

by:jguy07
ID: 17858392
The "Finally"clause is causing the compiler to throw this exception:
Operator '=' is not defined for types 'System.Data.SqlClient.SqlConnection' and 'System.Data.SqlClient.SqlConnection'.

How can I check if "ds.Tables(0).Rows.Count > 0" is actually returning any rows?  I hard code the "auth_user" value in the sql statement and remove the var and still do not return any rows.  Any ideas?  Thank you so much for the help thus far!  
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858529
ds.Tables(0).Rows.Count  is the number of rows returned by the dataset
0
 

Author Comment

by:jguy07
ID: 17858818
YZlat,
I understand what the "ds.Tables(0).Rows.Count" is now; many thanks.  However, how can I "echo" (sorry for the PHP term) the value of ds.Tables(0)Rows.Count to the screen to assure that I'm actually getting a value greater than 0?  I think my problem is that the sql isn't returning anything.....
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17858836
assign its value to a label or just use response.write to write it to a page.

Why don't you try debugging and use watch window to see its value
0
 

Author Comment

by:jguy07
ID: 17859640
YZlat,
I echoed the sql string value to the browser and it looks correct.  However, I do not believe that the query is returning any results (row count) in the web form.  I cut and paste the query from browser to sql and I DO get desired result.  In php, we have a function called "die" to display error text if a connection can not be made to the database.  Does vb.net have such a function?  Also, you say "why don't you try debugging and use watch window to see its value".  I can turn debugging on, but I'm not sure what you mean by "watch window"?  I think the "IF ds.Tables(0).Rows.Count > 0 Then ..." is returning false or nothing at all.  
0
 

Author Comment

by:jguy07
ID: 17862325
YZlat,
BINGO!  I figured out what my problem was.  I had to set up permissions on the SQL server for the ASPNET user.  Anyway, your code worked beautifully!  Many thanks and peace be with you.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question