Populate a form from SQL

Posted by xrum on Stack Overflow See other posts from Stack Overflow or by xrum
Published on 2011-01-13T16:40:18Z Indexed on 2011/01/13 16:53 UTC
Read the original article Hit count: 352

Filed under:
|
|
|
|

Hi,

I am trying to populate a web from from a SQL table.

This is what I have right now, though I am not sure if it's the best way to do things, please give me suggestions:

Public Class userDetails
    Public address1 As String
    Public address2 As String
    Public city As String
    ...
...
...
End Class

Public Class clsPerson
    'set SQL connection
    Dim objFormat As New clsFormat
    Dim objConn As New clsConn()
    Dim connStr As String = objConn.getConn()
    Dim myConnection As New Data.SqlClient.SqlConnection(connStr)

    Public Function GetPersonDetails() As userDetails
        'connection and all other good stuff here

        Try
            ' Execute the command
            myConnection.Open()
            dr = myCommand.ExecuteReader()

            ' Make sure a record was returned
            If dr.Read() Then
                ' Create and Populate ApplicantDetails
                userDetails.address1 = dr("address1")
                userDetails.address2 = objFormat.CheckNull(dr("address2"))
                userDetails.city = objFormat.CheckNull(dr("city"))
                ....

            Else
                Err.Raise(4938, "clsUser", "Error in GetUserDetails - User Not Found")
            End If

            dr.Close()
        Finally
            myConnection.Close()
        End Try

        Return userDetails
    End Function

i then use GetPersonDetails() function in my backend to populate the form.

like so:

Dim userDetails as new userDetails
userdetails = getPersonDetails()

txtAddress.text = userdetails.address1

etc....

however, there are like 50 fields in the User db, and it seems like a lot of retyping... please help me find a better way to do this.

Thank you!

© Stack Overflow or respective owner

Related posts about .NET

Related posts about ASP.NET