Link to home
Start Free TrialLog in
Avatar of dearness
dearnessFlag for American Samoa

asked on

Creating classes in ASP.net that reference SQL server tables and return values

I am fairly new to ASP.net and want some expert help on best practice code design; I am designing in Visual Studio in express.  I am giving an example of a part of my system design and would appreciate some help on a few design concepts.  I have two power metering database tables set up, tblMETER which holds meter information and tblPOWER which holds 15 minute power meter readings. In my ASP.net program I want to use classes and objects to reference the many power meters.
This is what I think I should do, create a class called PowerMeter, set properties for NUMBER, DESCRIPTION, and DIVIDER. Now when I create an instance of the class for example PM06 (power meter 06), Firstly what is the best way to assign the property values to the new instance, Like this?
DIM PM06 as new PowerMeter
PM06.Number = “06”
PM06.Description = read the database referencing meter number 06
PM06.Divider = read the database referencing meter number 06
Secondly I want to display the total P_KW readings for the meter on a web page.
Do I create another property for the class and assign it the value such as
PM06.Reading = return calculated reading or is there some way of using a method within the class?
Providing some good points on getting a working system using best practices.
 User generated image
SOLUTION
Avatar of GlobaLevel
GlobaLevel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dearness

ASKER

GlobalLevel,
Thanks for the reply. I have been doing some reading and come up with this. Use a constructor routine and pass the meter number on creation of the meter. Use that meter number to get the description and divider with a function inside the class code from the database.
I this something that would normally be done?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Create the new power meter with its number
        Dim PM06 As New PowerMeter("06")
    End Sub

Imports System.Data.SqlClient
Public Class PowerMeter
    Private mNumber As String
    Private mDescription As String
    Private mDivider As Integer
    Private mDbConnection

    Public Sub New(ByVal Value As String)
        mNumber = Value

    End Sub

    ReadOnly Property number() As String
        Get
            Return mNumber
        End Get

    End Property
    Property Description() As String
        Get
            Return mDescription
        End Get
        Set(ByVal value As String)
            mDescription = value
        End Set
    End Property

    Property Divider() As String
        Get
            Return mDivider
        End Get
        Set(ByVal value As String)
            mDivider = value
        End Set
    End Property

End Class

Open in new window

looks good...r u using inline code or code behind?

for inline:

1)Page directive....
2) <@ Import xxxx

3) <scriptxxx>
methods

</script>

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ddayx10,
I am going over you example.
Thanks for your input., Actually I was looking at posting a seperate question on the correct use of datalink layers. At this stage it is the use of objects I want to get right. Its the use of methods I am struggling with at the moment, when to use and how to use. Ideally I want all objects in the ASP.net project created as much as possible from the database tables.
ddayx10,
Its going to take a while to get my head around it but I am sure the principle is there for what I am after. I am having trouible getting it to run though, just give me while.
Not a big deal but I made a mistake of sorts in the PowerReadings class. I would change that name to just PowerReading if it was me. Then when I looped through the list it would make more sense(its a small thing but it bugs me).

Ex:

for each p as PowerReadings in PowerMeter.PowerReadings

VS.

for each p as PowerReading in PowerMeter.PowerReadings

The latter makes contextual sense.