Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

enumerating an objects properites 2

Status
Not open for further replies.

ghost807

IS-IT--Management
Jun 27, 2003
99
US
i have several classes that i have created
customer
job
material
truck

each class has a bunch of different properties
what i would like to do is create a generic database connection that i could use to update, retreive, delete, or add to the database.

now the question is how do i do this the best way (with oop in mind)


if i'm retreiving i send the name to the database class and it will pass back one of the objects.

if i'm adding or updating i pass the object to the database class and let it do it's work.


Finally i get the point of the message!!!!!
How do i pass an object to the database class and have it:
recognize what the object is
read each property and also it's value.

i don't know if what i'm asking is worth doing or not but right now i have to create a class(truck,customer,job,etc) and then create a class to handle the db stuff(dbcustomer,dbjob,dbtruck,etc)
i would like to remove the multiple dbclasses.
i think this should work because the tables i have created in the database have the same names and structures as the objects.


thanx for your imput.
(here is some code to help with the descriptions)

just a few button clicks to show how i'm implementing
Code:
    Private Sub btnJobFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJobFind.Click
        StatusBar.Text = "Retreiving Job Names"
        Dim Job As New clsJob
        Dim JobDB As New DBJob
        Job = JobDB.GetJob(txtJobFind.Text)
        With Job
            Me.txtJobNumber.Text = .JobNumber
            Me.txtJobDescription1.Text = .Description(1)
            Me.txtJobDescription2.Text = .Description(2)
            Me.txtJobDescription3.Text = .Description(3)
            Me.txtJobDescription4.Text = .Description(4)
            Me.txtJobComment1.Text = .Comment(1)
            Me.txtJobComment2.Text = .Comment(2)
            Me.txtJobComment3.Text = .Comment(3)
            Me.txtJobComment4.Text = .Comment(4)
        End With
        StatusBar.Text = ""
        txtCustomer.Text = ""
        Me.txtJobFind.Text = ""
    End Sub

Code:
    Private Sub btnJobUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJobUpdate.Click
        Dim Job As New clsJob
        Dim JobDB As New DBJob
        With Job
            .JobNumber = Me.txtJobNumber.Text
            .Description(1) = Me.txtJobDescription1.Text
            .Description(2) = Me.txtJobDescription2.Text
            .Description(3) = Me.txtJobDescription3.Text
            .Description(4) = Me.txtJobDescription4.Text
            .Comment(1) = Me.txtJobComment1.Text
            .Comment(2) = Me.txtJobComment2.Text
            .Comment(3) = Me.txtJobComment3.Text
            .Comment(4) = Me.txtJobComment4.Text
        End With
        JobDB.UpdateJob(Job)
        Me.txtJobNumber.Text = ""
        Me.txtJobDescription1.Text = ""
        Me.txtJobDescription2.Text = ""
        Me.txtJobDescription3.Text = ""
        Me.txtJobDescription4.Text = ""
        Me.txtJobComment1.Text = ""
        Me.txtJobComment2.Text = ""
        Me.txtJobComment3.Text = ""
        Me.txtJobComment4.Text = ""

    End Sub

this is my job object

Code:
#Region "Constants"
    Private myJobNumber As String
    Const MAXDESCRIPTION As Integer = 4
    Const MAXCOMMENT As Integer = 4
    Const MAX_UDF As Integer = 10

    Private myDescription(MAXDESCRIPTION) As String
    Private myComment(MAXCOMMENT) As String
    Private myUDF(MAX_UDF) As String
#End Region

#Region "Properties"
    Public Property JobNumber() As String
        Get
            Return myJobNumber
        End Get
        Set(ByVal Value As String)
            myJobNumber = Value
        End Set
    End Property

    Public Property Description(ByVal index As Integer) As String
        Get
            Return myDescription(index)
        End Get
        Set(ByVal Value As String)
            myDescription(index) = Value
        End Set
    End Property

    Public Property Comment(ByVal index As Integer) As String
        Get
            Return myComment(index)
        End Get
        Set(ByVal Value As String)
            myComment(index) = Value
        End Set
    End Property

    Public Property UDF(ByVal index As Integer) As String
        Get
            Return myUDF(index)
        End Get
        Set(ByVal Value As String)
            myUDF(index) = Value
        End Set
    End Property

#End Region
the dbconnection workhorse
Code:
Public Class DBJob
    Public Function GetJob(ByVal JobNumber As String) As clsJob
        Try
            Dim IDX As Integer = 0
            Dim job As New clsJob
            Dim Ds As New TestAppDB
            Dim Connect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
            Application.ExecutablePath.Substring(0, Application.ExecutablePath.Length - 11) & "testappdb.mdb"
            Dim adapter As New OleDbDataAdapter("SELECT * FROM tblJob WHERE JobNumber LIKE " & _
            Chr(34) & "%" & JobNumber & "%" & Chr(34), Connect)
            adapter.Fill(Ds, "tblJob")


            For IDX = 0 To Ds.tblJob.Rows.Count - 1
                Dim idx1 As Integer
                For idx1 = 0 To Ds.tblJob.Columns.Count - 1
                    If Ds.tblJob(IDX).Item(idx1) Is DBNull.Value Then
                        Ds.tblJob(IDX).Item(idx1) = ""
                    End If
                Next

                With job
                    .JobNumber = Ds.tblJob(IDX).JobNumber
                    .Description(1) = Ds.tblJob(IDX).Description1
                    .Description(2) = Ds.tblJob(IDX).Description2
                    .Description(3) = Ds.tblJob(IDX).Description3
                    .Description(4) = Ds.tblJob(IDX).Description4
                    .Comment(1) = Ds.tblJob(IDX).Comment1
                    .Comment(2) = Ds.tblJob(IDX).Comment2
                    .Comment(3) = Ds.tblJob(IDX).Comment3
                    .Comment(4) = Ds.tblJob(IDX).Comment4
                    .UDF(1) = Ds.tblJob(IDX).UDF1
                    .UDF(2) = Ds.tblJob(IDX).UDF2
                    .UDF(3) = Ds.tblJob(IDX).UDF3
                    .UDF(4) = Ds.tblJob(IDX).UDF4
                    .UDF(5) = Ds.tblJob(IDX).UDF5
                    .UDF(6) = Ds.tblJob(IDX).UDF6
                    .UDF(7) = Ds.tblJob(IDX).UDF7
                    .UDF(8) = Ds.tblJob(IDX).UDF8
                    .UDF(9) = Ds.tblJob(IDX).UDF9
                    .UDF(10) = Ds.tblJob(IDX).UDF10
                End With
            Next
            Ds.Dispose()
            adapter.Dispose()
            Return job
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function

    Public Sub UpdateJob(ByVal Job As clsJob)
        Try
            Dim Connect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
            Application.ExecutablePath.Substring(0, Application.ExecutablePath.Length - 11) & "testappdb.mdb"

            Dim conn As New OleDbConnection(Connect)
            Dim adapter As New OleDbDataAdapter
            Dim SQLString As String = "SELECT * FROM tblJob WHERE JobNumber = " & Chr(34) & Job.JobNumber & Chr(34)
            adapter.SelectCommand = New OleDbCommand(SQLString, conn)
            Dim cmdbuilder As New OleDbCommandBuilder(adapter)
            conn.Open()
            Dim JobUpdate As New TestAppDB
            adapter.TableMappings.Add("Table", "tblJob")
            adapter.Fill(JobUpdate.Tables("tblJob"))
            Dim idx As Integer
            For idx = 0 To JobUpdate.tblJob.Rows.Count - 1
                With Job
                    If Job.JobNumber.ToUpper = JobUpdate.tblJob(idx).JobNumber.ToUpper Then
                        JobUpdate.tblJob(idx).Description1 = .Description(1)
                        JobUpdate.tblJob(idx).Description2 = .Description(2)
                        JobUpdate.tblJob(idx).Description3 = .Description(3)
                        JobUpdate.tblJob(idx).Description3 = .Description(4)
                        JobUpdate.tblJob(idx).Comment1 = .Comment(1)
                        JobUpdate.tblJob(idx).Comment2 = .Comment(2)
                        JobUpdate.tblJob(idx).Comment3 = .Comment(3)
                        JobUpdate.tblJob(idx).Comment4 = .Comment(4)
                        JobUpdate.tblJob(idx).UDF1 = .UDF(1)
                        JobUpdate.tblJob(idx).UDF2 = .UDF(2)
                        JobUpdate.tblJob(idx).UDF3 = .UDF(3)
                        JobUpdate.tblJob(idx).UDF4 = .UDF(4)
                        JobUpdate.tblJob(idx).UDF5 = .UDF(5)
                        JobUpdate.tblJob(idx).UDF6 = .UDF(6)
                        JobUpdate.tblJob(idx).UDF7 = .UDF(7)
                        JobUpdate.tblJob(idx).UDF8 = .UDF(8)
                        JobUpdate.tblJob(idx).UDF9 = .UDF(9)
                        JobUpdate.tblJob(idx).UDF10 = .UDF(10)

                        Dim idx1 As Integer
                        For idx1 = 0 To JobUpdate.tblJob.Columns.Count - 1
                            If JobUpdate.tblJob(idx).Item(idx1) Is "" Then
                                JobUpdate.tblJob(idx).Item(idx1) = DBNull.Value
                            End If
                        Next
                    End If
                End With
            Next
            adapter.Update(JobUpdate)
            adapter.Dispose()
            JobUpdate.Dispose()
            conn.Dispose()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
 
YOu can use Reflection (System.Reflection namespace) and call a GetType() on your type to get a Type object. Once you have that, you can call GetProperties() to get a list of properties that it exposes. You can then iterate through that list, mapping property names to database column names.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I think chmohan s a man of little words and one URL.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
ok so far so good. the link you provided was fantastic.
but.......
i need to make it so that i can substitute in the table name that is taken from my class
Code:
            Dim myType As Type = AddItem.GetType
            Dim tblName As String = "tbl" & myType.Name.Substring(3)
            Dim cRow As TestAppDB.tblMaterialRow = addable.tblMaterial.NewtblMaterialRow

and i would need it to look something like this

Code:
            Dim myType As Type = AddItem.GetType
            Dim tblName As String = "tbl" & myType.Name.Substring(3)
            Dim cRow As TestAppDB.tblName & "Row" = addable.tblname.New & tblName & Row    'I know this isn't right but it helps explain what i'm trying to do.

but i cannot seem to figure out how to do that.
any suggestions.
 
ok all seems to be going well.
this is the last problem that i seem to be having.
thanx for your help so far.

any suggestions?


Code:
    Public Sub AddInfo(ByVal AddItem As Object)
        Dim myType As Type = AddItem.GetType
        Try
            Dim Connect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.ExecutablePath.Substring(0, Application.ExecutablePath.Length - 11) & "testappdb.mdb"
            Dim Conn As New OleDbConnection(Connect)
            Dim adapter As New OleDbDataAdapter

            Dim tblName As String = "tbl" & myType.Name.Substring(3)
            adapter.SelectCommand = New OleDbCommand("SELECT * FROM " & tblName, Conn)
            Dim cmdbuilder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)
            Conn.Open()

            Dim addable As New TestAppDB
            adapter.TableMappings.Add("Table", tblName)
            adapter.Fill(addable.Tables(tblName))

            Dim cRow As DataRow = addable.Tables(tblName).NewRow

            Dim myPropertyInfo As PropertyInfo() = myType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
            With AddItem
                Dim i As Integer
                For i = 0 To myPropertyInfo.Length - 1
                    Dim myPropInfo As PropertyInfo = CType(myPropertyInfo(i), PropertyInfo)
                    If myPropInfo.Name.ToString = "Comment" Then
                        cRow("comment1") = .Comment(1)
                        cRow("comment2") = .Comment(2)
                        cRow("comment3") = .Comment(3)
                        cRow("comment4") = .Comment(4)
                    ElseIf myPropInfo.Name.ToString = "Description" Then
                        cRow("Description1") = .Description(1)
                        cRow("Description2") = .Description(2)
                        cRow("Description2") = .Description(3)
                        cRow("Description4") = .Description(4)
                    ElseIf myPropInfo.Name.ToString = "UDF" Then
                        cRow("UDF1") = .UDF(1)
                        cRow("UDF2") = .UDF(2)
                        cRow("UDF3") = .UDF(3)
                        cRow("UDF4") = .UDF(4)
                        cRow("UDF5") = .UDF(5)
                        cRow("UDF6") = .UDF(6)
                        cRow("UDF7") = .UDF(7)
                        cRow("UDF8") = .UDF(8)
                        cRow("UDF9") = .UDF(9)
                        cRow("UDF10") = .UDF(10)
                    ElseIf myPropInfo.Name.ToString <> "UDF" And myPropInfo.Name.ToString <> "Description" And myPropInfo.Name.ToString <> "Comment" Then
                        [COLOR=red] cRow(myPropInfo.Name.ToString) = myPropInfo.GetValue([u]myType, (i)[/u][COLOR=green]*[/color])[/color]
[COLOR=green]*[/color][COLOR=blue]'this is the last problem that i am having. i keep receiving a couple of errors.
'cannot index 1 dimension array or
'getvalue does not accept this number of arguments[/color]
                    End If
                  
                Next i
            End With
            '        ''''''''''''''''''''''''''''''''''''''''''
            '        '''Check and set Nulls else the DB will Flip Out
            '        ''''''''''''''''''''''''''''''''''''''''''
            Dim idx As Integer
            For idx = 0 To addable.Tables(tblName).Columns.Count - 1
                If cRow.Item(idx) Is "" Then
                    cRow.Item(idx) = DBNull.Value
                End If

            Next
            ''''''''''''''''''''''''''''''''''''''''''
            '''We are done. Add the row
            ''''''''''''''''''''''''''''''''''''''''''
            addable.Tables(tblName).Rows.Add(cRow)
            adapter.Update(addable)
            Conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
 
no, i've been here several times to see what i'm missing. but i don't see it.
according to what i see there what i have now should work correctly.

i can change myType to myPropInfo or myPropertyInfo and i still get the same errors.

if i don't use an index i will get does not accept arguments.
it's gotta be something almost basic that i'm missing,
but not being smart enough it will continue to elude me until i'm bald.
 
C:\Documents and Settings\DaveEJr\My Documents\Visual Studio Projects\testapp\DBGeneric.vb(90): Value of type 'Integer' cannot be converted to '1-dimensional array of System.Object'.


i guess that's what i thought i was doing with (i) already at the begining of my for loop.
i guess i don't quite understand(not your fault, i'm just slow after lunch)
 
chmohan
i sat up last night thinking about what you told me to do.....
but still i don't understand.
everytime i try to do what you want i get integer cannot but 1 dimensional array.
 
look if this could work

myPropInfo.GetValue(myType, nothing)


Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
object does not match target type
but......................
if i change mytype to AddInfo then (sweet) it works

You guys are my hero's

everytime i have problems you are there to help out and point me in the right direction.




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top