4.How to Edit/Update data in the database?

Steps:
1.    Create CustomerUI.vb[Design] as shown below:
Make sure to name the controls similar to what is shown in CustomerUI.vb[code]

2.    On CustomerUI.vb[Code], type the following:
Imports MarioSoft.Sales.Customers
Imports MarioSoft.Sales.CustomerBAL
Public Class CustomerUI
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If AppToolBox.FieldsNotEmpty(Me.tabCustomer) = False Then Exit Sub
        Dim Customer As New Customers
        With Customer
            .ID = CInt(txtID.Text)
            .Name = txtName.Text
            .Address = txtAddress.Text
            .ContactNo = txtContactNo.Text
            .CreditLimit = CDec(txtCreditLimit.Text)
        End With
        CustomerBAL.Update(Customer)
        AppToolBox.Message("Save Successfully...")

        Dim s As System.Object = Nothing
        Dim ev As System.EventArgs = Nothing
        btnReset_Click(s, ev)
    End Sub
    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        AppToolBox.ClearTextBoxes(Me.tabCustomer)
        Me.txtID.Enabled = True
        Me.txtID.Focus()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class
3.    Create CustomerRecordUI.vb[Design] as shown below:

Control-Name-Text
Form-CustomerRecordUI
Listview-lvwCustomers
            View-Details
Gridlines-true
             FullRowSelect-True
             Columns-(Refer to the image)     
StatusStrip-stbCustomer
MenuStrip-mnuCustomer
MenuStripItem- RefreshToolStripMenuItem-Text-Refresh

4.    On CustomerRecordUI.vb[Code], type the following:
Public Class CustomerRecordUI
    Private Sub CustomerRecordUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetCustomers()
    End Sub
    Private Sub GetCustomers()
        Dim customers As List(Of Customers)
        customers = CustomerBAL.GetCustomers()
        Dim row As Integer = 0
        lvwCustomers.Items.Clear()
        For Each customer In customers
            lvwCustomers.Items.Add(customer.ID)
            With lvwCustomers.Items(row).SubItems
                .Add(customer.Name)
                .Add(customer.Address)
                .Add(customer.ContactNo)
                .Add(Format(customer.CreditLimit, "#,##0.00"))
            End With
            row = row + 1
        Next
        lblCustomers.Text = "Total Customers: " & customers.Count
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click
        GetCustomers()
    End Sub

    Private Sub lvwCustomers_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwCustomers.DoubleClick
        CustomerUI.txtID.Text = lvwCustomers.FocusedItem.Text
        CustomerUI.txtID.Enabled = False
        With lvwCustomers.FocusedItem
            CustomerUI.txtName.Text = .SubItems(1).Text
            CustomerUI.txtAddress.Text = .SubItems(2).Text
            CustomerUI.txtContactNo.Text = .SubItems(3).Text
            CustomerUI.txtCreditLimit.Text = .SubItems(4).Text
            CustomerUI.btnSave.Text = "Update"
        End With
        CustomerUI.ShowDialog()
    End Sub
End Class

5.    On Customers.vb class, type the following:
Public Class Customers
    Private mID As Integer
    Private mName As String
    Private mAddress As String
    Private mContactNo As String
    Private mCreditLimit As Decimal
    Property ID() As Integer
        Get
            Return mID
        End Get
        Set(ByVal value As Integer)
            mID = value
        End Set
    End Property
    Property Name() As String
        Get
            Return mName
        End Get
        Set(ByVal value As String)
            mName = value
        End Set
    End Property
    Property Address() As String
        Get
            Return mAddress
        End Get
        Set(ByVal value As String)
            mAddress = value
        End Set
    End Property
    Property ContactNo() As String
        Get
            Return mContactNo
        End Get
        Set(ByVal value As String)
            mContactNo = value
        End Set
    End Property
    Property CreditLimit() As Decimal
        Get
            Return mCreditLimit
        End Get
        Set(ByVal value As Decimal)
            mCreditLimit = value
        End Set
    End Property
End Class

6.    On CustomersBAL.vb class, type the following:
Imports MarioSoft.Sales.Customers
Imports MarioSoft.Sales.CustomersDAL
Public Class CustomerBAL
    Public Shared Sub Update(ByVal Customer As Customers)
        Dim CustomerDAL As New CustomersDAL
        CustomerDAL.Update(Customer)
    End Sub
    Public Shared Function GetCustomers() As List(Of Customers)
        Dim customerDAL As New CustomersDAL
        Return customerDAL.GetCustomers()
    End Function
End Class

7.    On CustomersDAL.vb class, type the following:
Imports MarioSoft.Sales.Customers
Imports System.Data.OleDb
Public Class CustomersDAL
    Dim db As OledbConnection
    Public Sub New()
        db = dbs.Connect
    End Sub
    Public Sub Update(ByVal customer As Customers)
        Dim sql As String
        sql = "UPDATE Customers SET name='" & customer.Name & "'," _
                                            & "address='" & customer.Address & "'," _
                                            & "contactno='" & customer.ContactNo & "'," _
                                            & "creditlimit='" & customer.CreditLimit & "'" _
                                            & "WHERE id=" & customer.ID & ""
        Execute(sql)
    End Sub
    Private Sub Execute(ByVal sql As String)
        db.Open()
        Dim cmd = New OleDbCommand(sql, db)
        cmd.ExecuteNonQuery()
        db.Close()
    End Sub
    Public Function GetCustomers() As List(Of Customers)
        Dim sql As String
        sql = "SELECT * FROM Customers ORDER BY name"
        Dim cmd As New OleDbCommand(sql, db)
        Dim oda As New OleDbDataAdapter(cmd)
        Dim dt As New DataTable
        oda.Fill(dt)

        Dim customers As New List(Of Customers)
        For Each DataRow In dt.Rows
            customers.Add(GetRow(DataRow))
        Next

        Return customers
    End Function

    Private Function GetRow(ByVal row As DataRow) As Customers
        Dim customer As New Customers
        customer.ID = row("id")
        customer.Name = row("name")
        customer.Address = row("address")
        customer.ContactNo = row("contactno")
        customer.CreditLimit = row("creditlimit")
        Return customer
    End Function
End Class

8.    On dbs.vb class, type the following:
Imports System.Data.OleDb
Public Class dbs
    Public Shared Function Connect() As OledbConnection
        Dim con As New OleDbConnection
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
        & "Data Source=" & Application.StartupPath & "\Database\SALES.MDB"
        Return con
    End Function
End Class

9.    On AppToolBox.vb module, type the following:
Module AppToolBox
    Function FieldsNotEmpty(ByVal c As Control) As Boolean
        FieldsNotEmpty = True

        Dim txbx As New TextBox
        For Each ctrl In c.Controls
            If ctrl.GetType.ToString = txbx.GetType.ToString Then
                If ctrl.text = "" Then
                    FieldsNotEmpty = False
                    MsgBox("Cannot continue, check for empty field.", MsgBoxStyle.Exclamation, "Message")
                    Exit For
                End If
            End If
        Next
    End Function
    Public Sub ClearTextBoxes(ByVal c As Control)
        Dim txbx As New TextBox
        For Each ctrl In c.Controls
            If ctrl.GetType.ToString = txbx.GetType.ToString Then
                ctrl.text = ""
            End If
        Next
    End Sub
    Public Sub Message(ByVal msg As String)
        MessageBox.Show(msg, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Module

10.  Run the program






No comments:

Post a Comment