Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET Databases Also see the forum ASP Databases for more general discussions of ASP database issues not directly related to these books.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 30th, 2004, 05:51 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Default Creating Classes

I have am trying to write a class that will perform Selection, Insert, Update and Delete. I copied the ProductInfo.vb class from chapter 10 but I don't understand a lot of what it is I am reading. Nor why the author wrote the code as he or she did. My attempt to convert what I dont understand to what I need is not working. Thus far I have the following:

Option Explicit On
Option Strict On

Imports System
Imports System.Collections
Imports System.Data
Imports System.Data.OleDb

Public Class clPerfromanceMeasures

#Region "Class fields"
    'Class Field Definitions the attributes follow"
    Private m_PerformanceMeasures As SortedList
    Private mSqlConnStr As String

    Private Const LOADPERFORMANCEMEASURESSTR As String = "SELECT PerfNum, TrackingDivisionCode, PerformanceMeasures, GroupCode FROM PerformanceMeasures ORDER BY TrackingDivisionCode;"
#End Region

#Region "Class Properties"
    Public ReadOnly Property SqlConnStr() As String
        Get
            Return mSqlConnStr
        End Get
    End Property

    Public Property PerformanceMeasures() As SortedList
        Get
            Return m_PerformanceMeasures
        End Get
        Set(ByVal Value As SortedList)
            m_PerformanceMeasures = Value
        End Set
    End Property

    Private Sub LoadPerformanceMeasures(ByVal SqlDR As OleDbDataReader)
        Try
            While
                Dim PerfNum As Integer = 0
                Dim TrackingDivisionCode As String = ""
                Dim PerformanceMeasures As String = ""
                Dim GroupCode As String = ""

                If Not SqlDR.IsDBNull(0) Then
                    PerfNum = SqlDR.GetInt32(0)
                End If
                If Not SqlDR.IsDBNull(1) Then
                    TrackingDivisionCode = SqlDR.GetString(1)
                End If
                If Not SqlDR.IsDBNull(2) Then
                    PerformanceMeasures = SqlDR.GetString(21)
                End If
                If Not SqlDR.IsDBNull(3) Then
                    GroupCode = SqlDR.GetString(3)
                End If

                m_PerformanceMeasures.Add(PerfNum, New PerformanceMeasure(PerfNum, TrackingDivisionCode, PerformanceMeasures, GroupCode))

            End While

            SqlDR.NextResult()

        Catch SqlExc As OleDbException
            Throw SqlExc
        Catch Exc As Exception
            Throw Exc
        End Try
    End Sub

    Private Function PrepareStringForSql(ByVal val As String) As String
        val = val.Trim()
        val = val.Replace("'", "''")
        Return val
    End Function

    Private Function BooleanToSqlBit(ByVal Flag As Boolean) As String
        If Flag Then
            Return "1"
        Else
            Return "0"
        End If
    End Function

    Private Function ValidateBusinessRules(ByVal PerfNum As Integer, _
    ByVal TrackingDivisionCode As String, _
    ByVal PerformanceMeasures As String, _
    ByVal GroupCode As String) As Boolean

        If PerformanceMeasures.Length = 0 Then
            Throw New Exception("A Performance Measure is required." & PerfNum.ToString())
        End If
        If PerformanceMeasures.Length > 250 Then
            Throw New Exception("A Performance Measure is less than 250 characters." & PerfNum.ToString())
        End If

    End Function

#End Region

#Region "Public Class Methods"
    Public Sub LoadFromDatabase()
        Dim conn As New OleDbConnection(mSqlConnStr)
        Dim cmd As OleDbCommand
        Dim sqlDR As OleDbDataAdapter

        If m_PerformanceMeasures.Count > 0 Then
            m_PerformanceMeasures.Clear()
        End If

        Try
            If mSqlConnStr.Length = 0 Then
                Throw New Exception("SQL Connection string cannot be zero-length.")
            End If
            conn.Open()

            cmd = New OleDbCommand(LOADPERFORMANCEMEASURESSTR, conn)
            cmd.CommandType = CommandType.Text
            sqlDR = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            LoadPerformanceMeasures(sqlDR)
            sqlDRClose()

        Catch SqlExc As OleDbException
            Throw SqlExc
        Catch Exc As Exception
            Throw Exc
        Finally
            If Not conn Is Nothing Then
                If conn.State <> ConnectionState.Closed Then
                    If Not cmd Is Nothing Then
                        cmd.Cancel()
                    End If
                    conn.Close()
                End If
            End If

        End Try
    End Sub
    Public Sub LoadFromDatabase(ByVal SqlConnStr As String)
        If SqlConnStr.Length > 0 Then
            mSqlConnStr = SqlConnStr
        End If
        Try
            LoadFromDatabase()

        Catch SqlExc As OleDbException
            Throw SqlExc
        Catch Exc As Exception
            Throw Exc
        End Try

    End Sub

    Public Function AddPerformanceMeasure(ByVal PerfNum As Integer, _
    ByVal TrackingDivisionCode As String, _
    ByVal PerformanceMeasures As String, _
    ByVal GroupCode As String) As Boolean
        Dim conn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim perform As PerformanceMeasure
        Dim newid As Integer

        If mSqlConnStr.Length = 0 Then
            Throw New Exception("Sql connection string cannot be zero-length")
        End If

        Try
            If ValidateBusinessRules(PerfNum, TrackingDivisionCode, PerformanceMeasures, GroupCode) Then

                cmd = New OleDbCommand()
                cmd.CommandType = CommandType.Text

                cmd.CommandText = "INSERT INTO PerformanceMeasures(PerfNum, TrackingDivisionCode, PerformanceMeasures, GroupCode) values & _"
                (" & _
                "'" & PrepareStringForSql(PerfNum.ToString()) & "',"& _
                TrackingDivisionCode.ToString() & "," & _
                PerformanceMeasures.ToString() & "," & _
                GroupCode.ToString() & ");"



            End If


        End Try

    End Function


#End Region
End Class

I stopped at this point. My database consists of three table the table this class creation is referring to contains, an autonumber and text data types.

I would TREMENDOUSLY appreciated direction on this! If there is a simple way to solve this that would be even better. I would like to open the databse, insert new records, close the database, minor validation and be done with it and it is not happening for me.

Thank You.

GailCG:(
Reply With Quote
  #2 (permalink)  
Old April 27th, 2005, 10:42 PM
bg bg is offline
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Which part don't you understand?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using classes seymour_glass C# 1 April 26th, 2007 11:18 PM
About Classes. wdw BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 July 29th, 2005 09:46 AM
Dynamically Creating Objects of Array of Classes dinkarsinha General .NET 0 January 28th, 2005 06:22 AM
Creating classes!! NitinJoshi General .NET 2 January 24th, 2005 10:44 AM
Creating new objects from existing classes module0000 Beginning VB 6 3 June 17th, 2003 08:13 AM



All times are GMT -4. The time now is 10:01 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.