Click to See Complete Forum and Search --> : Custom object shouldn't have to reload data


HairyMonkeyMan
November 15th, 2005, 05:13 AM
Hello all,

I am writing an application based roughly around the timetracker project.

Users are logged in using their nt logins (domain/user), and the login to the sql server is through a trusted connection.

The object I am talking about is called 'CurrentUser.vb', and here it is:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb

Namespace MND.PMacs.BusinessLogicLayer

Public Class CurrentUser
Private _NTLogin As String
Private Shared _FullName As String
Private Shared _PMacsID As String

Public Sub New(ByVal NTLogin As String)
_NTLogin = NTLogin

Dim dbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("MyDbConn1").ToString)
Dim dbRead As OleDbDataReader
Dim dbComm As New OleDbCommand("SELECT StaffRef, FirstName + ' ' + SurName AS Name FROM Staff " & _
"WHERE LoginID = ?", dbConn)
dbComm.Parameters.Add(New OleDbParameter("LoginID", _NTLogin))
dbComm.Connection.Open()

dbRead = dbComm.ExecuteReader

While dbRead.Read
_PMacsID = dbRead.Item(0).ToString
_FullName = dbRead.Item(1).ToString
End While

dbComm.Connection.Close()
End Sub

Public Shared Property FullName() As String
Get
Return _FullName
End Get
Set(ByVal Value As String)
_FullName = Value
End Set
End Property

Public Shared Property PMacsID() As String
Get
Return _PMacsID
End Get
Set(ByVal value As String)
_PMacsID = value
End Set
End Property

End Class ' CurrentUser
End Namespace ' MND.PMacs.BusinessLogicLayer

I am creating and calling this object from a masterpage, like so:

<%@ Master Language="VB" %>
<%@ Import Namespace="MND.PMacs.BusinessLogicLayer" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">
Dim _CurrentUser As CurrentUser

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

If Not Page.IsPostBack Then
_CurrentUser = New CurrentUser(Page.User.Identity.Name)
End If
End Sub ' Page Load
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Project Management & Accounting System</title>
<link type="text/css" href="Styles.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div class="header">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td rowspan="2" class="logo" width="177"><img src="./img/logo.gif" /></td>
<td align="right" class="headertext">
<%="Welcome, <b>" & MND.PMacs.BusinessLogicLayer.CurrentUser.FullName & "</b>"%>


The problem with this is that every time I refresh a page (with this masterpage as the master) the user information is re-fetched from the database.

What I am trying to accomplish here is to have the user information fetched once, then stored in the object.

Any ideas whats wrong?

Thanks for your time :thumb:

HairyMonkeyMan
November 18th, 2005, 02:15 PM
Anybody? :(

mmetzger
November 18th, 2005, 03:56 PM
I haven't had a chance to touch ASP.NET 2.0 yet, but my guess is it's not interpreting the IsPostBack correctly.