0

I learnt this programming from other sources, and I also downloaded his file to test is it works. I did everything same with him, but mine is not works and not coming out any errors. Could any senior help me to do some correction or debug.

This is my code:

Dim Command As New OleDb.OleDbCommand
Dim Adapter As New OleDb.OleDbDataAdapter
Dim Connection As OleDb.OleDbConnection = AConnection()

Public Function AConnection() As OleDb.OleDbConnection
    Return New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Music_Sales_Database.mdb")
End Function

Private Sub SendCustomerInformationToDatabase()
    Connection.Open()
    With Command
        .Connection = Connection
        .CommandText = "INSERT into Customer(Customer_ID,First_Name,Last_Name,Contact_Number,Email_Address) " & _
            "Values('" & txtCustomerID.Text & "','" & txtFirstName.Text & "','" & txtLastName.Text & "','" & txtContactNumber.Text & "','" & _
        "','" & txtEMail.Text & "')"

    End With
    Connection.Close()
End Sub

Private Sub CheckOut_Reservation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    txtDateOfReservation.Text = Date.Today
    txtCustomerID.Focus()
    InputText = txtContactNumber.Text
    NumericCheck = IsNumeric(InputText)

    Connection.Open()
    With Command
        .Connection = Connection
        .CommandText = "SELECT * from Customer"
    End With
    Dim Table As New DataTable

    Adapter.SelectCommand = Command
    Adapter.Fill(Table)

    Connection.Close()
    Adapter.Dispose()

End Sub

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

    SendCustomerInformationToDatabase()

    Dim CheckOutSound As String
    Dim path = System.Windows.Forms.Application.StartupPath
    Dim MyPlayer As New SoundPlayer()

    CheckOutSound = "\Success.wav"
    MyPlayer.SoundLocation = path & CheckOutSound
    MyPlayer.Play()

    If AInputCorrect = True And BInputCorrect = True And CInputCorrect = True And DInputCorrect = True And EInputCorrect = True Then
        FInputCorrect = True
    Else
        FInputCorrect = False
    End If

    If FInputCorrect = True Then
        MessageBox.Show("Reservation Submitted.", "Reservation Successful", MessageBoxButtons.OK, MessageBoxIcon.None)
    End If

    If FInputCorrect = False Then
        MessageBox.Show("Please check your Customer Information and try again.", "Reservation Failed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End If

    'Me.Close()
End Sub

Thank You

2
  • 1
    Please do not build strings, it is very dangerous. Use a parameter query, for example, stackoverflow.com/questions/12954039/… You do not have to use question marks, but you must get the parameters in the right order.
    – Fionnuala
    Apr 7, 2014 at 12:16
  • 1
    what error are you getting and where OR what doesnt it do? 'its not working' is not very helpful...you do build a SQL statement and then not execute it (send it to the DB) Apr 7, 2014 at 12:16

1 Answer 1

0

As far as i'm seeing, you're not executing your command:

Private Sub SendCustomerInformationToDatabase()
    Connection.Open()
    With Command
        .Connection = Connection
        .CommandText = "INSERT into Customer(Customer_ID,First_Name,Last_Name,Contact_Number,Email_Address) " & _
         "Values('" & txtCustomerID.Text & "','" & txtFirstName.Text & "','" & txtLastName.Text & "','" & txtContactNumber.Text & "','" & _
         "','" & txtEMail.Text & "')"

    End With

'Execute my newly created command
    Command.ExecuteNonQuery
    Connection.Close()
End Sub

MSDN - ExecuteNonQuery

Do know that , you might encounter further exceptions if your query contains Syntax-Errors.

Please remember that structuring your SQL-Query like that is really a BAD Practice. I would advise you to a take a look at the following links

Stack Overflow: Preventing SQL Injection

Code Project: Sql Injection Attacks

Using Parameterized queries

2
  • What about the injection vulnerability?
    – Fionnuala
    Apr 7, 2014 at 13:25
  • That as well but its the reason why it won't execute. I've added some links with regards to this though
    – User999999
    Apr 7, 2014 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.