Click here to Skip to main content
16,004,602 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralDisconnected Data: Help! Pin
Rommel the iCeMAn17-Nov-04 11:04
Rommel the iCeMAn17-Nov-04 11:04 
GeneralRe: Disconnected Data: Help! Pin
R. Thomas17-Nov-04 19:36
R. Thomas17-Nov-04 19:36 
GeneralRe: Disconnected Data: Help! Pin
Rommel the iCeMAn18-Nov-04 2:55
Rommel the iCeMAn18-Nov-04 2:55 
GeneralLook for custom Printing/Report Pin
AstronusX17-Nov-04 9:59
AstronusX17-Nov-04 9:59 
Generaladdins for photoshop Pin
shinay17-Nov-04 8:19
shinay17-Nov-04 8:19 
GeneralVisual Basic and Microsoft Access Pin
underb@asd20.org17-Nov-04 8:18
underb@asd20.org17-Nov-04 8:18 
GeneralOLD TAPI CONTROL Pin
John R. Shaw17-Nov-04 6:28
John R. Shaw17-Nov-04 6:28 
Generalproblem filling object properties (error: 'System.Reflection.TargetException: Object does not match target type') Pin
kobber17-Nov-04 6:23
kobber17-Nov-04 6:23 
Hi there,

I'm trying to create a DAL. Therefore I'm analyzing the DotNetNuke project and extracting code to create my own DAL. Thusfar I managed to talk to the database (with DataProvider.Instance). Now I'm trying to use a FillObject method, but I seem to be stuck at this point. I'm getting the following error 'System.Reflection.TargetException: Object does not match target type'. The error occurs in the sub CreateObject (last catch) at the following line 'CType(objProperties(intProperty), PropertyInfo).SetValue(objObject, Null.SetNull(CType(objProperties(intProperty), PropertyInfo)), Nothing)'. I was trying to isolate the problem (in sub Testing), but I can't figure out what's going wrong. I'm new to this stuff (that is working with a DAL, filling objects automatically) and I'm not an DotNet expert. Could someone explain to me what's going wrong? Does it have anything to do with trying to fill a wrong instance? Here's my code:


Imports DB
Imports DB.Data

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Configuration
Imports System.Data
Imports System.Data.OleDb
Imports System.Reflection
Imports System.Web
Imports System.Web.Mail
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Xml

Namespace Message

Public Class Contact : Inherits Page

Public lblStatus As Label
Public dg1 As DataGrid

Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim msg As New MessageController
Dim messageid As Integer = 52

dg1.DataSource = DataProvider.Instance.GetMessage(messageid)
dg1.DataBind

Dim m As MessageInfo = CType(FillObject2(DataProvider.Instance().GetMessage(messageID), GetType(MessageInfo)), MessageInfo)
'lblStatus.Text = "test" & m.Content

' Testing(GetType(MessageInfo))
End Sub

Public Sub Testing(ByVal objType As Type)
Dim objObject As Object = Activator.CreateInstance(objType)
Dim intProperty As Integer = 4
Dim objProperties As ArrayList = GetPropertyInfo2(objType)
Dim bla As System.String = "bla"
Dim pType As Type = CType(objProperties(intProperty), PropertyInfo).PropertyType

'Mypropertyinfo.SetValue(objObject, bla, Nothing)
'CType(objProperties(intProperty), PropertyInfo).SetValue(objObject, Convert.ChangeType("hallo", pType), Nothing)

Dim MypropertyInfo As PropertyInfo = CType(objProperties(intProperty), PropertyInfo)
Response.Write("
GetValue: " & MypropertyInfo.GetValue(objObject, Nothing).ToString())
' Mypropertyinfo.SetValue(objObject, "This caption has been changed.", Nothing)
' Response.Write("
GetValue: " & Mypropertyinfo.GetValue(Myproperty, Nothing).ToString())
Response.Write(MypropertyInfo.ToString)
End Sub


Public Sub PrintValues(myList As IEnumerable)
Dim myEnumerator As System.Collections.IEnumerator = myList.GetEnumerator()
While myEnumerator.MoveNext()
Response.Write(myEnumerator.Current.ToString & "
")
End While
End Sub

Public Function FillObject2(ByVal dr As IDataReader, ByVal objType As Type) As Object
Dim objFillObject As Object
Dim intProperty As Integer
' get properties for type
Dim objProperties As ArrayList = GetPropertyInfo2(objType)
' get ordinal positions in datareader
Dim arrOrdinals As Integer() = GetOrdinals2(objProperties, dr)

PrintValues(objProperties)

For i As Integer = 0 To arrOrdinals.GetUpperBound(0)
Response.Write(arrOrdinals(i) & "
")
Next

' read datareader
If dr.Read Then
' fill business object
objFillObject = CreateObject2(objType, dr, objProperties, arrOrdinals)
Else
objFillObject = Nothing
End If

' close datareader
If Not dr Is Nothing Then
dr.Close()
End If

Return objFillObject
End Function

Public Function GetPropertyInfo2(ByVal objType As Type) As ArrayList
' Use the cache because the reflection used later is expensive
Dim objProperties As ArrayList = CType(DataCache.GetCache(objType.FullName), ArrayList)

If objProperties Is Nothing Then
objProperties = New ArrayList
Dim objProperty As PropertyInfo

For Each objProperty In objType.GetProperties()
objProperties.Add(objProperty)
Next
DataCache.SetCache(objType.FullName, objProperties)
End If

Return objProperties
End Function

Private Function GetOrdinals2(ByVal objProperties As ArrayList, ByVal dr As IDataReader) As Integer()
Dim arrOrdinals(objProperties.Count) As Integer
Dim intProperty As Integer

If Not dr Is Nothing Then
For intProperty = 0 To objProperties.Count - 1
arrOrdinals(intProperty) = -1
Try
arrOrdinals(intProperty) = dr.GetOrdinal(CType(objProperties(intProperty), PropertyInfo).Name)
Catch
' property does not exist in datareader
End Try
Next intProperty
End If

Return arrOrdinals
End Function

Private Function CreateObject2(ByVal objType As Type, ByVal dr As IDataReader, ByVal objProperties As ArrayList, ByVal arrOrdinals As Integer()) As Object
Dim objObject As Object = Activator.CreateInstance(objType)
Dim intProperty As Integer

' fill object with values from datareader
For intProperty = 0 To objProperties.Count - 1
If CType(objProperties(intProperty), PropertyInfo).CanWrite Then
If arrOrdinals(intProperty) <> -1 Then
If IsDBNull(dr.GetValue(arrOrdinals(intProperty))) Then
' translate Null value
CType(objProperties(intProperty), PropertyInfo).SetValue(objObject, Null.SetNull(CType(objProperties(intProperty), PropertyInfo)), Nothing)
Else
Try
' try implicit conversion first
CType(objProperties(intProperty), PropertyInfo).SetValue(objObject, dr.GetValue(arrOrdinals(intProperty)), Nothing)
Catch ' data types do not match
Try
Dim pType As Type = CType(objProperties(intProperty), PropertyInfo).PropertyType
'need to handle enumeration conversions differently than other base types
If pType.BaseType.Equals(GetType(System.Enum)) Then
CType(objProperties(intProperty), PropertyInfo).SetValue(objObject, System.Enum.ToObject(pType, dr.GetValue(arrOrdinals(intProperty))), Nothing)
Else
' try explicit conversion
CType(objProperties(intProperty), PropertyInfo).SetValue(objObject, Convert.ChangeType(dr.GetValue(arrOrdinals(intProperty)), pType), Nothing)
' Response.Write(pType.ToString & "
" & objObject.ToString)
' Response.Write("
" & Convert.ChangeType(dr.GetValue(arrOrdinals(intProperty)), pType).ToString & "

")
End If
Catch
' error assigning a datareader value to a property
CType(objProperties(intProperty), PropertyInfo).SetValue(objObject, Null.SetNull(CType(objProperties(intProperty), PropertyInfo)), Nothing)
End Try
End Try
End If
Else ' property does not exist in datareader
CType(objProperties(intProperty), PropertyInfo).SetValue(objObject, Null.SetNull(CType(objProperties(intProperty), PropertyInfo)), Nothing)
End If
End If
Next intProperty

Return objObject
End Function
End Class 'Contact

Public Class MessageInfo

'local property declarations
Private _ID As Integer
Private _UserID As Integer
Private _CreatedDate As Date
Private _Category As String
Private _Subject As String = "test"
Private _Content As String

'initialization
Public Sub New()
End Sub

'public properties
Public Property ID() As Integer
Get
Return _ID
End Get
Set(ByVal Value As Integer)
_ID = Value
End Set
End Property

Public Property UserID() As Integer
Get
Return _UserID
End Get
Set(ByVal Value As Integer)
_UserID = Value
End Set
End Property

Public Property CreatedDate() As Date
Get
Return _CreatedDate
End Get
Set(ByVal Value As Date)
_CreatedDate = Value
End Set
End Property

Public Property Category() As String
Get
Return _Category
End Get
Set(ByVal Value As String)
_Category = Value
End Set
End Property

Public Property Subject() As String
Get
Return _Subject
End Get
Set(ByVal Value As String)
_Subject = Value
End Set
End Property

Public Property Content() As String
Get
Return _Content
End Get
Set(ByVal Value As String)
_Content = Value
End Set
End Property

End Class 'MessageInfo

Public Class MessageController

Public Function GetMessage(ByVal messageID As Integer ) As MessageInfo

Return CType(CBO.FillObject(DataProvider.Instance().GetMessage(messageID), GetType(MessageInfo)), MessageInfo)

End Function

End Class 'MessageController

End Namespace


GeneralReference dll not found ! Pin
ronenw17-Nov-04 5:07
ronenw17-Nov-04 5:07 
GeneralRe: Reference dll not found ! Pin
MohammadAmiry18-Nov-04 0:11
MohammadAmiry18-Nov-04 0:11 
GeneralRe: Reference dll not found ! Pin
ronenw20-Nov-04 19:47
ronenw20-Nov-04 19:47 
GeneralCreating a Help File Pin
cwayman17-Nov-04 5:02
cwayman17-Nov-04 5:02 
GeneralRe: Creating a Help File Pin
Rommel the iCeMAn18-Nov-04 3:10
Rommel the iCeMAn18-Nov-04 3:10 
GeneralIExtractImage... Pin
schweet_nectur17-Nov-04 3:57
schweet_nectur17-Nov-04 3:57 
GeneralPrint Dialog Pin
partt17-Nov-04 3:41
partt17-Nov-04 3:41 
GeneralRe: Print Dialog Pin
Dave Kreskowiak17-Nov-04 15:44
mveDave Kreskowiak17-Nov-04 15:44 
GeneralLPCTSTR Question Pin
SarahAmelia17-Nov-04 3:21
SarahAmelia17-Nov-04 3:21 
GeneralRe: LPCTSTR Question Pin
Mike Ellison17-Nov-04 6:50
Mike Ellison17-Nov-04 6:50 
GeneralRe: LPCTSTR Question Pin
SarahAmelia17-Nov-04 7:47
SarahAmelia17-Nov-04 7:47 
GeneralRe: LPCTSTR Question Pin
Mike Ellison17-Nov-04 7:52
Mike Ellison17-Nov-04 7:52 
GeneralRe: LPCTSTR Question Pin
SarahAmelia17-Nov-04 8:04
SarahAmelia17-Nov-04 8:04 
GeneralRe: LPCTSTR Question Pin
MohammadAmiry17-Nov-04 23:31
MohammadAmiry17-Nov-04 23:31 
GeneralRe: LPCTSTR Question Pin
SarahAmelia18-Nov-04 1:08
SarahAmelia18-Nov-04 1:08 
Generalparsing query in data report in runtime Pin
anj198317-Nov-04 0:55
anj198317-Nov-04 0:55 
GeneralPosting data to php file Pin
celebisson17-Nov-04 0:39
celebisson17-Nov-04 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.