2. How to save data to the database?

Steps
1.     On VB.NET Solution Explorer, create  the following folders/files structure as shown below:

2.     Create a database named SALES.mdb on MS Access and save it to:
Location of your files above then…MarioSoft.Sales\bin\Debug\Database\

3.     On SALES database SQL View, type the following script below :
CREATE TABLE Customers
(
id integer,
name text(50),
address text(70),
contactno text(11),
creditlimit currency
)
Click Run/Execute button to execute the script

4.     On your CustomerUI.vb[Design] create the UI as shown below:

Make sure to name the controls similar to what is shown in CustomerUI.vb[code]

5.     On your CustomerUI.vb[code]
a.     double click the button Save and type the following starting from If AppToolBox…

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.Save(Customer)
        AppToolBox.Message("Save Successfully...")
        AppToolBox.ClearTextBoxes(Me.tabCustomer)
        Me.txtID.Focus()
            End Sub

b.    double click the button Reset and type the following starting from  AppToolBox…
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
                        AppToolBox.ClearTextBoxes(Me.tabCustomer)
                        Me.txtID.Focus()
            End Sub

c.     double click the button Close and type Me.Close()
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
                        Me.Close()
            End Sub
End Class

6.     On your Customers.vb, 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

7.     On your CustomersBAL.vb, type the following:
Imports MarioSoft.Sales.Customers
Imports MarioSoft.Sales.CustomersDAL
Public Class CustomerBAL
    Public Shared Sub Save(ByVal Customer As Customers)
        Dim CustomerDAL As New CustomersDAL
        CustomerDAL.Save(Customer)
    End Sub
End Class

8.     On your dbs.vb, 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 your CustomersDAL.vb, 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 Save(ByVal Customer As Customers)
        Dim sql As String
        sql = "INSERT INTO Customers" _
        & "(id,name,address,contactno,creditlimit)VALUES(" _
        & "'" & Customer.ID & "','" & Customer.Name & "'," _
        & "'" & Customer.Address & "','" & Customer.ContactNo & "'," _
        & "'" & Customer.CreditLimit & "')"
        db.Open()
        Dim cmd = New OleDbCommand(sql, db)
        cmd.ExecuteNonQuery()
        db.Close()
    End Sub
End Class

10.  On your AppToolBox.vb, 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


11. Run your program by clicking start debugging[F5] button on the toolbar.


Testing your code
-Click save while all or other textbox is empty, notice what happen.
-This time input all textboxes with valid values, and click Save button.
-To check the data you inputted if it is really stored, check your customers table on your database.

Debugging your code
-To familiarize the behaviour of the language or to know how your codes are executed in sequence, debugging should be done. In visual basic, just put a break point by clicking at the left margin of the procedure event or function you would like to trace and then press F8. To break the debugging mode, just click the Stop Debugging button at the right of the start debugging button in the toolbar. To remove the break point, just click the break point to toggle.





1 comment:

  1. sir, i have a question?
    what would be the connection string to your database if you are going to use Microsoft Access instead of using Visual Data Manager? Im planning to use Access instead of installing VisualBasic6.0. Thanks :)

    ReplyDelete