?
Solved

ASP.NET:  How to set sessions from a query.

Posted on 2005-05-04
2
Medium Priority
?
397 Views
Last Modified: 2010-04-07
I used to use loop in classic asp, but not sure the best method to populate a users info from a query.  Sample code.  Please no external links.  VB code preferred.


Code:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Page Language="VB" Debug="true"%>
<script runat="server">
    Sub Page_Load(Source As Object, E as EventArgs)
            Dim strDomain as String  = User.Identity.Name
            Dim strLogon As String
            Dim strIndx As String
            strIndx = InStr (strDomain, "\" )
            If strIndx = 0 Then
              strLogon = strDomain
            Else
              strLogon = Right(strDomain, strIndx)
            End If

            Dim objConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=c:\inetpub\wwwroot\OT\OT.mdb")
           Dim strSQL As String = "SELECT Managers.Logon FROM Managers WHERE Logon = ('" & strLogon & "')"
           Dim ds as DataSet=New DataSet()
           Dim cmdValidate as New OLEDBDataAdapter(strSQL, objConn)
           cmdValidate.Fill(ds,"Managers")
           Dim RcdCount
           RcdCount = DS.Tables("Managers").Rows.Count.ToString()
           If RcdCount = 0 Then
            lblMessage.Text = "You are not authorized to view this site"
           Else
            MakeSession()
           ' Response.Redirect("Main.aspx")
            lblMessage.Text = Session("Fname") + Session("FName")          
           End If
    End Sub
    Sub MakeSession()
            Dim objConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=c:\inetpub\wwwroot\OT\OT.mdb")
           Dim strSQL As String = "SELECT * FROM Managers WHERE Logon = ('" & Session("Logon") & "')"
           Dim ds as DataSet=New DataSet()
           Dim cmdValidate as New OLEDBDataAdapter(strSQL, objConn)
           cmdValidate.Fill(ds,"Managers")
        'Session("LName") = DataGrid.Items(0).Cells(0).Text
        Session("FName") = DataGrid.Items(1).Cells(1).Text
        'Session("SAP") = DataGrid.Items(0).Cells(2).Text
        'Session("Logon") = DataGrid.Items(0).Cells(3).Text
        'Session("Site") = DataGrid.Items(0).Cells(4).Text
        'Session("Organization") = DataGrid.Items(0).Cells(5).Text
        'Session("Department") = DataGrid.Items(0).Cells(6).Text
    End Sub

</script>
<b><asp:Label id="lblMessage" runat="server" /></b>
                <asp:Datagrid
            id="datagrid"
            runat="server"
            Width="95%"
                Visible="False" />
0
Comment
Question by:Mose_
2 Comments
 
LVL 6

Expert Comment

by:dharmesh_amity
ID: 13927477
 Dim objConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=c:\inetpub\wwwroot\OT\OT.mdb")
           Dim strSQL As String = "SELECT * FROM Managers WHERE Logon = ('" & Session("Logon") & "')"
           Dim ds as DataSet=New DataSet()
           Dim cmdValidate as New OLEDBDataAdapter(strSQL, objConn)
           cmdValidate.Fill(ds,"Managers")
        Session("LName") = ds.Rows[0]["LastName"]
     
0
 
LVL 8

Accepted Solution

by:
thrill_house earned 2000 total points
ID: 13927624
You can also do it this way.  By using a datareader.

Dim objConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=c:\inetpub\wwwroot\OT\OT.mdb")
Dim strSQL As String = "SELECT * FROM Managers WHERE Logon = ('" & Session("Logon") & "')"

Dim myCommand as new OleDbCommand(strSQL, objConn)
objConn.open()
Dim myReader as OleDbDataReader = myCommand.executeReader
objConn.close()

If myReader.Read() then
      Session("LName") = myReader.item("LastName")
      Session("FName") = myReader.item("FirstName")
      ....
End If
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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 …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

830 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