Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access_asp thread: Using Oledbdatareader with Dates


Message #1 by "Michael Lynch" <Visionct@E...> on Tue, 4 Feb 2003 01:19:14
Hello All
    I been trying to retrieve data from a date field in access, and load
it into an array. There are 21 records which is fixed and will never 
change. I've found many example of retrieving string data using examples
like below but none retrieving anything but strings.

 Do While objDataReader.Read()=True
    strResultsHolder +=objDataREader("FirstName")
    strResultsHolder +="&nbsp;"
    strResultsHolder +=objDataREader("LastName")
    strNameArray(counter) = objDataREader("LastName")
    strResultsHolder +="<br>"
  Loop

I've attempted many different angle with no avail. I've posted some of my 
code to give an idea of what I'm trying to do. Any help would be 
appreciated. Thanks Mike


<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Oledb" %>

<script language="vb" runat="server">
Sub Page_Load()
  Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;"
    strConnection += "Data Source=C:\BegASPNET\VBall2003.mdb"
    data_src.text = strConnection
  Dim strSQL as string = "SELECT Date FROM ResultsWM"
  Dim datPlayDate(21) as Date
  Dim counter as Integer

  Dim objConnection as New OledbConnection(strConnection)
  Dim objCommand as New OledbCommand(strSQL, objConnection)
  Dim objDataReader as OledbDataReader

  try
    objConnection.Open()
    con_open.text="Connection opened successfully.<br />"
    objDataReader = objCommand.ExecuteREader()

    for counter = 1 to 21
      datPlayDate(counter) = objDataREader(Date)
    next counter

    objDataReader.Close()
    objConnection.Close()
    con_close.text="Connection closed.<br>"
  catch e as Exception
    con_open.text="Connection failed to open successfully.<br>"
    con_close.text=e.ToString()
  end try
end Sub
Message #2 by "Ken Schaefer" <ken@a...> on Tue, 4 Feb 2003 16:23:04 +1100
You are supplying the name of the field as a *string*, so you need to supply
a string representation of the name of the field:

datPlayDate(counter) = objDataReader("Date")

If you want to use a variable then:

Dim strFieldName as String
strFieldName = "Date"
datPlayDate(counter) = objDateReader(strFieldName)

One last point: Date is a reserved word. It is a VBA function in Access that
retrieves the current system date. Do *not* gives your tables or fields the
same name. Otherwise, it will come back to bite you later on. See (for
example):

http://www.adopenstatic.com/faq/80040e14.asp

Cheers
Ken


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Michael Lynch" <Visionct@E...>
Subject: [access_asp] Using Oledbdatareader with Dates


: Hello All
:     I been trying to retrieve data from a date field in access, and load
: it into an array. There are 21 records which is fixed and will never
: change. I've found many example of retrieving string data using examples
: like below but none retrieving anything but strings.
:
:  Do While objDataReader.Read()=True
:     strResultsHolder +=objDataREader("FirstName")
:     strResultsHolder +="&nbsp;"
:     strResultsHolder +=objDataREader("LastName")
:     strNameArray(counter) = objDataREader("LastName")
:     strResultsHolder +="<br>"
:   Loop
:
: I've attempted many different angle with no avail. I've posted some of my
: code to give an idea of what I'm trying to do. Any help would be
: appreciated. Thanks Mike
:
:
: <%@ import Namespace="System.Data" %>
: <%@ import Namespace="System.Data.Oledb" %>
:
: <script language="vb" runat="server">
: Sub Page_Load()
:   Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;"
:     strConnection += "Data Source=C:\BegASPNET\VBall2003.mdb"
:     data_src.text = strConnection
:   Dim strSQL as string = "SELECT Date FROM ResultsWM"
:   Dim datPlayDate(21) as Date
:   Dim counter as Integer
:
:   Dim objConnection as New OledbConnection(strConnection)
:   Dim objCommand as New OledbCommand(strSQL, objConnection)
:   Dim objDataReader as OledbDataReader
:
:   try
:     objConnection.Open()
:     con_open.text="Connection opened successfully.<br />"
:     objDataReader = objCommand.ExecuteREader()
:
:     for counter = 1 to 21
:       datPlayDate(counter) = objDataREader(Date)
:     next counter
:
:     objDataReader.Close()
:     objConnection.Close()
:     con_close.text="Connection closed.<br>"
:   catch e as Exception
:     con_open.text="Connection failed to open successfully.<br>"
:     con_close.text=e.ToString()
:   end try
: end Sub

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Message #3 by "Mike Lynch" <Visionct@E...> on Tue, 4 Feb 2003 06:53:09 -0500
Thank you for your reply
My problem is the date is stored as a variable type Date in Access. Is
there
Anyway to retrieve it in that form. Thanks for your thoughts about using
date as a field name.
    Mike


  Return to Index