Solved

Global.asa TO Global.asax

Posted on 2006-11-01
13
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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