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

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" />
Mose_Asked:
Who is Participating?
 
thrill_houseConnect With a Mentor Commented:
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
 
dharmesh_amityCommented:
 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
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.