dcsimg
Results 1 to 5 of 5

Thread: CRUD using class in VB.NET

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Posts
    102

    CRUD using class in VB.NET

    Hello, I am not really fond of using classes but I knew I have to learn them as well. So now I am creating a simple project using CRUD and this is what I have so far. I am trying to do the insert to access database here but I don't know how to continue. This is what's on my class file:

    Code:
    Public Class PendingTicks
        Inherits ObjectBase
    
        Private _IncidentNum As String
        Public Property InNumber As String
            Get
                Return _IncidentNum
            End Get
            Set(ByVal value As String)
                _IncidentNum = value
            End Set
        End Property
    
        Private _IncidentType As String
        Public Property InType As String
            Get
                Return _IncidentType
            End Get
            Set(ByVal value As String)
                _IncidentType = value
            End Set
        End Property
    
        Private _IncidentStatus As String
        Public Property InStatus As String
            Get
                Return _IncidentStatus
            End Get
            Set(ByVal value As String)
                _IncidentStatus = value
            End Set
        End Property
    
        Private _IncidentSource As String
        Public Property InSource As String
            Get
                Return _IncidentSource
            End Get
            Set(ByVal value As String)
                _IncidentSource = value
            End Set
        End Property
    
        Private _DateLogged As String
        Public Property InDate As String
            Get
                Return _DateLogged
            End Get
            Set(ByVal value As String)
                _DateLogged = value
            End Set
        End Property
    
        Private _StartTime As String
        Public Property InStart As String
            Get
                Return _StartTime
            End Get
            Set(ByVal value As String)
                _StartTime = value
            End Set
        End Property
    
        Private _Timer As String
        Public Property InTimer As String
            Get
                Return _Timer
            End Get
            Set(ByVal value As String)
                _Timer = value
            End Set
        End Property
    
        Private _Notes As String
        Public Property InNotes As String
            Get
                Return _Notes
            End Get
            Set(ByVal value As String)
                _Notes = value
            End Set
        End Property
    
        Private _AnalystName As String
        Public Property InAnalyst As String
            Get
                Return _AnalystName
            End Get
            Set(ByVal value As String)
                _AnalystName = value
            End Set
        End Property
    
        Private _Remarks As String
        Public Property InRemarks As String
            Get
                Return _Remarks
            End Get
            Set(ByVal value As String)
                _Remarks = value
            End Set
        End Property
    
        Private _Comments As String
        Public Property InComments As String
            Get
                Return _Comments
            End Get
            Set(ByVal value As String)
                _Comments = value
            End Set
        End Property
    
        Public Sub New()
            Me.Create = "INSERT INTO tbl_AHT_PendingTickets ([IncidentNum], [IncidentType], [IncidentStatus], [IncidentSource], [DateLogged], [StartTime], [Timer], [Notes], [AnalystName], [Remarks], [Comments]) _
                    VALUES (@InNum, @InType, @InStat, @InSource, @InDate, @InStart, @InTimer, @InNotes, @InAnalyst, @InRemarks, @InComments)"
        End Sub
    
        Public Sub SavePending()
            Try
                Using AConn As New OleDb.OleDbConnection(Me.ConnectionString)
                    Using insertToDB As New OleDb.OleDbCommand(Me.Create, AConn)
                        insertToDB.Parameters.Add("@InNum", OleDb.OleDbType.VarChar, 100).Value = _IncidentNum
                        insertToDB.Parameters.Add("@InType", OleDb.OleDbType.VarChar, 20).Value = _IncidentType
                        insertToDB.Parameters.Add("@InStat", OleDb.OleDbType.VarChar, 20).Value = _IncidentStatus
                        insertToDB.Parameters.Add("@InSource", OleDb.OleDbType.VarChar, 20).Value = _IncidentSource
                        insertToDB.Parameters.Add("@InDate", OleDb.OleDbType.VarChar, 20).Value = _DateLogged
                        insertToDB.Parameters.Add("@InStart", OleDb.OleDbType.VarChar, 20).Value = _StartTime
                        insertToDB.Parameters.Add("@InTimer", OleDb.OleDbType.VarChar, 20).Value = _Timer
                        insertToDB.Parameters.Add("@InNotes", OleDb.OleDbType.VarChar, 500).Value = _Notes
                        insertToDB.Parameters.Add("@InAnalyst", OleDb.OleDbType.VarChar, 50).Value = _AnalystName
                        insertToDB.Parameters.Add("@InRemarks", OleDb.OleDbType.VarChar, 500).Value = _Remarks
                        insertToDB.Parameters.Add("@InComments", OleDb.OleDbType.VarChar, 500).Value = _Comments
                        insertToDB.Connection.Open()
                        insertToDB.ExecuteNonQuery()
                    End Using
                End Using
    
            Catch ex As Exception
                Throw
            End Try
        End Sub
    End Class

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,116

    Re: CRUD using class in VB.NET

    The first thing that got my attention is that you are using the long-winded way of writing properties. When you are just setting/returning the value of a private variable (and not doing any other work), you can use the short version... so instead of this:
    Code:
        Private _IncidentNum As String
        Public Property InNumber As String
            Get
                Return _IncidentNum
            End Get
            Set(ByVal value As String)
                _IncidentNum = value
            End Set
        End Property
    ...you can use this simple equivalent:
    Code:
        Public Property InNumber As String
    (and then use InNumber in the rest of the class, instead of _IncidentNum)


    The next thing is that you are attempting to use two properties/variables/consts that don't exist: Me.Create and Me.ConnectionString

    As Me.Create seems to be a constant value, it should be declared as a Const, and as it seems to be explicitly for SavePending it should be declared inside that routine.

    I'm not sure what your intentions are with Me.ConnectionString , but there is a good chance that it would be appropriate for it to be a Const declared at class level.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Posts
    102

    Re: CRUD using class in VB.NET

    hello, I just did not include my connection string

    So should I do it this way:

    Code:
    Public Class PendingTicks
        Inherits ObjectBase
    
        Public Property InNumber As String
        Public Property InType As String
        Public Property InStatus As String
        Public Property InSource As String
        Public Property InDate As String
        Public Property InStart As String
        Public Property InTimer As String
        Public Property InNotes As String
        Public Property InAnalyst As String
        Public Property InRemarks As String
        Public Property InComments As String
    
        Public Sub New()
            Me.Create = "INSERT INTO tbl_AHT_PendingTickets ([IncidentNum], [IncidentType], [IncidentStatus], [IncidentSource], [DateLogged], [StartTime], [Timer], [Notes], [AnalystName], [Remarks], [Comments]) _
                    VALUES (@InNum, @InType, @InStat, @InSource, @InDate, @InStart, @InTimer, @InNotes, @InAnalyst, @InRemarks, @InComments)"
        End Sub
    
        Public Sub SavePending()
            Try
                Using AConn As New OleDb.OleDbConnection(Me.ConnectionString)
                    Using insertToDB As New OleDb.OleDbCommand(Me.Create, AConn)
                        insertToDB.Parameters.Add("@InNum", OleDb.OleDbType.VarChar, 100).Value = InNumber
                        insertToDB.Parameters.Add("@InType", OleDb.OleDbType.VarChar, 20).Value = InType
                        insertToDB.Parameters.Add("@InStat", OleDb.OleDbType.VarChar, 20).Value = InStatus
                        insertToDB.Parameters.Add("@InSource", OleDb.OleDbType.VarChar, 20).Value = InSource
                        insertToDB.Parameters.Add("@InDate", OleDb.OleDbType.VarChar, 20).Value = InDate
                        insertToDB.Parameters.Add("@InStart", OleDb.OleDbType.VarChar, 20).Value = InStart
                        insertToDB.Parameters.Add("@InTimer", OleDb.OleDbType.VarChar, 20).Value = InTimer
                        insertToDB.Parameters.Add("@InNotes", OleDb.OleDbType.VarChar, 500).Value = InNotes
                        insertToDB.Parameters.Add("@InAnalyst", OleDb.OleDbType.VarChar, 50).Value = InAnalyst
                        insertToDB.Parameters.Add("@InRemarks", OleDb.OleDbType.VarChar, 500).Value = InRemarks
                        insertToDB.Parameters.Add("@InComments", OleDb.OleDbType.VarChar, 500).Value = InComments
                        insertToDB.Connection.Open()
                        insertToDB.ExecuteNonQuery()
                    End Using
                End Using
    
            Catch ex As Exception
                Throw
            End Try
        End Sub
    End Class

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,116

    Re: CRUD using class in VB.NET

    The properties are much easier to read (and behave the same way), so that's better.

    As the rest is the same, my other comments still apply.

  5. #5
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,272

    Re: CRUD using class in VB.NET

    you say you want CRUD so you need more methods. you can implement a .Create, .Read, .Update and .Delete Method but i tend to do it that way:
    Code:
    public sub new (id as int32)
      'db select and transfer fo rowdata to private vars
    end sub
    
    public sub new()
      'used to create a new record
    end sub
    
    public sub Write()
      'if new INSERT, if existing UPDATE
    end sub
    
    public sub delete()
      'DELETE
    end sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width