Can't Convert Oracle DataReader to SafeDataReader

Can't Convert Oracle DataReader to SafeDataReader

Old forum URL: forums.lhotka.net/forums/t/3863.aspx


elfstoneUK posted on Thursday, November 08, 2007

Ladies and Gentlemen,

your help please.

I have been using CSLA for about three weeks now and read most of the book (up to chapter 9). I'm using VB 2005 and an Oracle 10g DB in a Windows Forms design.

I'm in the process of building a prototype for the rest of the team. This is very much in the line with the Project Tracker example from the download.

So far so good. But I have hit a brick wall. I can't seem to convert from OracleDataReader to the SafeDataReader. I want to take advantage of the implicit null checking.

I have searched the forum but I can't see any resolution to this.

Can anyone tell if this can be done and how?

I have tried CType, DirectCast which compile but give an InvalidCast error at runtime!

Thanks for reading.

 

 

 

elfstoneUK replied on Thursday, November 08, 2007

Here is the error:

Unable to cast object of type 'Oracle.DataAccess.Client.OracleDataReader' to type 'Csla.Data.SafeDataReader'."}

System.InvalidCastException: {"Unable to cast object of type 'Oracle.DataAccess.Client.OracleDataReader' to type 'Csla.Data.SafeDataReader'

McManus replied on Thursday, November 08, 2007

Hi,

You cannot create a SafeDataReader by casting. You should call the constructor of SafeDataReader and pass in the OracleDataReader:

Dim command as OracleCommand
Dim safeReader as SafeDataReader

safeReader = New SafeDataReader(command.ExecuteReader)

Cheers,
Herman

elfstoneUK replied on Thursday, November 08, 2007

Herman,

Thanks for your reply. It worked!  Fantastic. Big Smile [:D]

So we get SafeDataReader and Smartdate all from a few lines of code.

I understand what's happening but I couldn't see in until you explained it. Simple really.  Big Smile [:D]

JoeFallon1 replied on Thursday, November 08, 2007

You can create a subclass of SafeDataReader in your BO project so that you can override methods to help you work with Oracle. As I recall, Oracle does not have Bit field like SQL Server does so there is a problem with GetBoolean in SafeDataReader. I use a config file setting to switch between SQL Server and Oracle. I also code my DAL to use IDataReader and return SQLDataReader or OracleDataReader based on the same setting.

Here is my version:

<Serializable()> _
Public Class MySafeDataReader
 
Inherits SafeDataReader

#Region " Class Variable Declarations "
 
 
Private mDBType As String = WebConfigurationManager.AppSettings("DBType")

  Public Sub New(ByVal dataReader As IDataReader)
   
MyBase.New(dataReader)
 
End Sub

#End Region

#Region " Properties and Methods "

  Public Overrides Function GetBoolean(ByVal i As Integer) As Boolean
   
If mDBType = "SQL Server" Then
     
If DataReader.IsDBNull(i) Then
       
Return False
     
Else
       
Return DataReader.GetBoolean(i)
     
End If
   
ElseIf mDBType = "Oracle" Then
     
If DataReader.IsDBNull(i) Then
       
Return False
     
Else
       
If CStr(DataReader(i)) = "1" Then
         
Return True
       
Else
         
Return False
       
End If
     
End If
   
End If
End Function

#End Region

End Class

Joe

elfstoneUK replied on Thursday, November 08, 2007

Thanks Joe Smile [:)]

I'm interested to learn how would you do this for the app.config?

JoeFallon1 replied on Thursday, November 08, 2007

I am not sure what you are asking here.

It is just a setting in the config file which you read on app startup.

<appSettings file="user.config">
 
<add key="Authentication" value="CSLA"/>
 
<add key="DBType" value="SQL Server"/>
  <!--
<add key="DBType" value="Oracle" /> -->
 
<add key="ConnectionString" value="data source=joe;database=myDB;uid=myUser;password=myPwd"/>
</appSettings>

Joe

Public Shared ReadOnly Property AppSettings() As System.Collections.Specialized.NameValueCollection

Member of: System.Web.Configuration.WebConfigurationManager

Summary:

Gets the System.Configuration.AppSettingsSection data for the current Web application's default configuration.

Return Values:

A System.Collections.Specialized.NameValueCollection object containing the System.Configuration.AppSettingsSection object for the current Web application's default configuration.

Exceptions:

System.Configuration.ConfigurationErrorsException: A valid System.Collections.Specialized.NameValueCollection object could not be retrieved with the application settings data.

elfstoneUK replied on Wednesday, November 14, 2007

Of course it is, thanks Joe Smile [:)]

Copyright (c) Marimer LLC