Solved

Global.asa TO Global.asax

Posted on 2006-11-01
13
509 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
Comment Utility
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
Comment Utility
Sub Session_End()
Session("authuser")= Nothing
Session("name")= Nothing
Session("num")= Nothing
end Sub
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
place the above inside your Global.asax file
0
 

Author Comment

by:jguy07
Comment Utility
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
Comment Utility
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
Comment Utility
this line checks if dataset returns any rows:
 If ds.Tables(0).Rows.Count > 0 Then
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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
Comment Utility
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
Comment Utility
ds.Tables(0).Rows.Count  is the number of rows returned by the dataset
0
 

Author Comment

by:jguy07
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now