...
I tried to mimic your setup but I still get the option to
provide a format string. I just know this is gonna be
something so simple we'll both kick ourselves.
Hi Jan, ok - my apologies for the delay - the following is my code, its a
bit lengthly for a group posting but I figured you'd wanna see it all
really..please also note that this is a work in progress so some things
aren't finished yet (although feel free to comment upon anything you thinks
a bit poor)...you might wanna paste this into VS to make it reformat a bit
better...
Basically, when I log in, I run a function to get my the user (GetUser) -
which I pass my username/password to (I mention this only because there are
2 GetUser functions below).
This returns me my "User" object - the ProfileLastUpdated has been populated
with a date from SQL Server and it includes the time.
I then populate my session variables accordingly; for the one in question:
Session("ProfileLastUpdated") = User.ProfileLastUpdated
This should currently hold a "date"
On my "UpdateProfile.aspx" page, if the page is not a post back then I
display a label which should have a date and time in it:
lblProfileLastUpdated.Text = "last updated " & <my_value_goes_here>
So, if I do this:
lblProfileLastUpdated.Text = "last updated " & Session("ProfileLastUpdated")
I get this on the page:
"last updated 23/05/2006 19:17:21" for example...
Now - the objective of all of this was to make it a bit more friendly,
initially I wanted this format "dd/MM/yyyy HH:mm" - so just losing the
seconds - so I tried this:
lblProfileLastUpdated.Text = "last updated " &
User.ProfileLastUpdated.ToString(" <-- the intellisense at this point
displays and I get a the following in the box
Chars (index As Integer) As Char
index:
A character position in this instance.
Now - here's where it gets even more fruity....
If I type this:
lblProfileLastUpdated.Text = "last updated " &
User.ProfileLastUpdated.ToString().ToString( <-- the intellisense at this
point displays and I get two options...
I can have .ToString() which says it just returns the instance of a string
and no conversion is made, or
I can have .ToString(provider as System.IFormatProvider)
"Provider: (reserved) An System.IFormatProvider that provides culture
specific formating information."
I hope all of this makes some sense to you - as at no point does it seem
happy to just do this:
lblProfileLastUpdated.Text = "last updated " &
User.ProfileLastUpdated.ToString("dd/MM/yyyy HH:mm")
as I would have liked and expected...
User.vb
Imports Microsoft.VisualBasic
Public Class User
' class variables
Private _userID As Integer
Private _userForename As String
Private _userSurname As String
Private _userEmailAddress As String
Private _userPermissionID As Integer
Private _userPermissionName As String
Private _userUsername As String
Private _userProfileLastUpdated As Date
' instantiate
Public Sub New()
End Sub
' properties
Public Property ID() As Integer
Get
Return _userID
End Get
Set(ByVal value As Integer)
_userID = value
End Set
End Property
Public Property Forename() As String
Get
Return _userForename
End Get
Set(ByVal value As String)
_userForename = value
End Set
End Property
Public Property Surname() As String
Get
Return _userSurname
End Get
Set(ByVal value As String)
_userSurname = value
End Set
End Property
Public ReadOnly Property FullName() As String
Get
Return _userForename & " " & _userSurname
End Get
End Property
Public Property EmailAddress() As String
Get
Return _userEmailAddress
End Get
Set(ByVal value As String)
_userEmailAddress = value
End Set
End Property
Public Property PermissionID() As Integer
Get
Return _userPermissionID
End Get
Set(ByVal value As Integer)
_userPermissionID = value
End Set
End Property
Public Property PermissionName() As String
Get
Return _userPermissionName
End Get
Set(ByVal value As String)
_userPermissionName = value
End Set
End Property
Public Property Username() As String
Get
Return _userUsername
End Get
Set(ByVal value As String)
_userUsername = value
End Set
End Property
Public Property ProfileLastUpdated() As Date
Get
Return _userProfileLastUpdated
End Get
Set(ByVal value As Date)
_userProfileLastUpdated = value
End Set
End Property
' methods
End Class
DataAccess.vb (note I've stripped out all the methods that were not related
to the poplation of the User.vb class)
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Public Class DataAccess
' class variables
Private _homeMovieDatabaseConnectionString As String
' instantiate
Public Sub New(ByVal connectionString As String)
_homeMovieDatabaseConnectionString = connectionString
End Sub
' methods
Public Function GetUser(ByVal username As String, ByVal password As String)
As User
' declare variables
Dim Connection As SqlConnection
Dim Command As SqlCommand
Dim Parameters As ArrayList
Dim DataAdapter As SqlDataAdapter
Dim DataTable As DataTable
Dim User As User
' create new instances of our objects
User = New User
Parameters = New ArrayList
' add parameter to parameters
Parameters.Add(New SqlParameter("@Username", username))
Parameters.Add(New SqlParameter("@Password", password))
' create new instances of our objects
Connection = GetConnection()
Command = GetCommand(Connection, "GetUserByUsernameAndPassword",
CommandType.StoredProcedure, Parameters)
DataAdapter = GetDataAdapter(Command)
DataTable = GetDataTable()
' execute command and fill data adapter
DataAdapter.Fill(DataTable)
' check to see if we returned a row
If DataTable.Rows.Count > 0 Then
' populate
User.ID = DataTable.Rows(0).Item("UserID")
User.Forename = DataTable.Rows(0).Item("UserForename")
User.Surname = DataTable.Rows(0).Item("UserSurname")
User.EmailAddress = DataTable.Rows(0).Item("UserEmailAddress")
User.ProfileLastUpdated = DataTable.Rows(0).Item("UserProfileLastUpdated")
User.PermissionID = DataTable.Rows(0).Item("PermissionID")
User.PermissionName = DataTable.Rows(0).Item("PermissionName")
User.Username = DataTable.Rows(0).Item("LogonUsername")
Else
' clear object
User = Nothing
End If
' tidy up
Parameters = Nothing
Command.Dispose()
Command = Nothing
Connection.Close()
Connection = Nothing
' return result
Return User
End Function
Public Function GetUser(ByVal userID As Integer) As User
' declare variables
Dim Connection As SqlConnection
Dim Command As SqlCommand
Dim Parameters As ArrayList
Dim DataAdapter As SqlDataAdapter
Dim DataTable As DataTable
Dim User As User
' create new instances of our objects
User = New User
Parameters = New ArrayList
' add parameter to parameters
Parameters.Add(New SqlParameter("@UserID", userID))
' create new instances of our objects
Connection = GetConnection()
Command = GetCommand(Connection, "GetUserByUserID",
CommandType.StoredProcedure, Parameters)
DataAdapter = GetDataAdapter(Command)
DataTable = GetDataTable()
' execute command and fill data adapter
DataAdapter.Fill(DataTable)
' check to see if we returned a row
If DataTable.Rows.Count > 0 Then
' populate
User.ID = DataTable.Rows(0).Item("UserID")
User.Forename = DataTable.Rows(0).Item("UserForename")
User.Surname = DataTable.Rows(0).Item("UserSurname")
User.EmailAddress = DataTable.Rows(0).Item("UserEmailAddress")
User.ProfileLastUpdated = DataTable.Rows(0).Item("UserProfileLastUpdated")
User.PermissionID = DataTable.Rows(0).Item("PermissionID")
User.PermissionName = DataTable.Rows(0).Item("PermissionName")
User.Username = DataTable.Rows(0).Item("LogonUsername")
Else
' clear object
User = Nothing
End If
' tidy up
Parameters = Nothing
Command.Dispose()
Command = Nothing
Connection.Close()
Connection = Nothing
' return result
Return User
End Function
Private Function GetConnection() As SqlConnection
' declare variables
Dim Connection As SqlConnection
' create a new instance of our object
Connection = New SqlConnection(_homeMovieDatabaseConnectionString)
' open connection
Connection.Open()
' return connection
Return Connection
End Function
Private Function GetCommand(ByVal connection As SqlConnection, ByVal
commandText As String, ByVal commandType As System.Data.CommandType) As
SqlCommand
' declare variables
Dim Command As SqlCommand
' create a new instance of our object
Command = New SqlCommand
' define command properties
Command.Connection = connection
Command.CommandText = commandText
Command.CommandType = commandType
' return command
Return Command
End Function
Private Function GetCommand(ByVal connection As SqlConnection, ByVal
commandText As String, ByVal commandType As System.Data.CommandType, ByVal
parameters As ArrayList) As SqlCommand
' declare variables
Dim Command As SqlCommand
Dim Parameter As SqlParameter
' create a new instance of our object
Command = New SqlCommand
' define command properties
Command.Connection = connection
Command.CommandText = commandText
Command.CommandType = commandType
' add parameters
For Each Parameter In parameters
Command.Parameters.Add(Parameter)
Next
' return command
Return Command
End Function
Private Function GetDataAdapter(ByVal command As SqlCommand) As
SqlDataAdapter
' declare variables
Dim DataAdapter As SqlDataAdapter
' create a new instance of our object
DataAdapter = New SqlDataAdapter(command)
' return data adapter
Return DataAdapter
End Function
Private Function GetDataTable() As DataTable
' declare variables
Dim DataTable As DataTable
' create a new instance of our object
DataTable = New DataTable
' return data table
Return DataTable
End Function
End Class