Global.asa TO Global.asax

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?  
jguy07Asked:
Who is Participating?
 
YZlatConnect With a Mentor Commented:
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
 
YZlatCommented:
Sub Session_End()
Session("authuser")= Nothing
Session("name")= Nothing
Session("num")= Nothing
end Sub
0
 
YZlatCommented:
place the above inside your Global.asax file
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
jguy07Author Commented:
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
 
jguy07Author Commented:
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
 
YZlatCommented:
this line checks if dataset returns any rows:
 If ds.Tables(0).Rows.Count > 0 Then
0
 
YZlatCommented:
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
 
jguy07Author Commented:
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
 
YZlatCommented:
ds.Tables(0).Rows.Count  is the number of rows returned by the dataset
0
 
jguy07Author Commented:
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
 
YZlatCommented:
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
 
jguy07Author Commented:
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
 
jguy07Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.