bwraths
bwraths

Reputation: 7

can anybody help me solve this error "OleDbException was unhandled"?

This part of the code is to let lecturer publish consultation schedule, unfortunately i experienced another error as shown in title, after done asking a question at Can anybody help me to solve this error "NullReferenceException was unhandled"? ,and that error is probably resolved. Now, my problem is when i clicked on the publish button which is like saving all the labels(that has the color lime as shown in the form) to an access file, and those labels already declared as(lime = available, red = unavailable).

This is the form

The error is at Dim f As Integer = cmd.ExecuteNonQuery()

    Public Class ConsultationSchedule

Private Sub publishbutton_Click(sender As Object, e As EventArgs) Handles publishbutton.Click
    Dim label As String = "Label"

    Dim time1 As String = vbNull
    Dim time2 As String = vbNull
    Dim time3 As String = vbNull
    Dim time4 As String = vbNull
    Dim time5 As String = vbNull

    Dim day1 As String = vbNull
    Dim day2 As String = vbNull
    Dim day3 As String = vbNull
    Dim day4 As String = vbNull
    Dim day5 As String = vbNull

    Dim available1 As String = vbNull
    Dim available2 As String = vbNull
    Dim available3 As String = vbNull
    Dim available4 As String = vbNull
    Dim available5 As String = vbNull

    For i = 11 To 15
        time1 = Label7.Text

        For k = 16 To 20
            time2 = Label8.Text

            For t = 21 To 25
                time3 = Label9.Text

                For u = 26 To 30
                    time4 = Label10.Text

                    For bg = 33 To 37
                        time5 = Label32.Text

                    Next
                Next
            Next
        Next
    Next

    For i = 11 To 37
        If (i = 11) Then
            day1 = Label2.Text
            If Not Me.Controls(label & i.ToString) Is Nothing Then
                If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                    available1 = "Available"
                Else
                    available1 = "Unavailable"
                End If
            End If

        ElseIf (i = 16) Then
            day1 = Label2.Text
            If Not Me.Controls(label & i.ToString) Is Nothing Then
                If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                    available2 = "Available"
                Else
                    available2 = "Unavailable"
                End If
            End If

        ElseIf (i = 21) Then
            day1 = Label2.Text
            If Not Me.Controls(label & i.ToString) Is Nothing Then
                If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                    available3 = "Available"
                Else
                    available3 = "Unavailable"
                End If
            End If

        ElseIf (i = 26) Then
            day1 = Label2.Text
            If Not Me.Controls(label & i.ToString) Is Nothing Then
                If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                    available4 = "Available"
                Else
                    available4 = "Unavailable"
                End If
            End If

        ElseIf (i = 33) Then
            day1 = Label2.Text
            If Not Me.Controls(label & i.ToString) Is Nothing Then
                If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                    available5 = "Available"
                Else
                    available5 = "Unavailable"
                End If
            End If
        End If
    Next
    For ht = 11 To 37
        If (ht = 11) Then
            day1 = Label2.Text
        ElseIf (ht = 16) Then
            day1 = Label2.Text
        ElseIf (ht = 21) Then
            day1 = Label2.Text
        ElseIf (ht = 26) Then
            day1 = Label2.Text
        ElseIf (ht = 33) Then
            day1 = Label2.Text
        End If
    Next
    For k = 11 To 37
        If (k = 12) Then
            day2 = Label3.Text
        ElseIf (k = 17) Then
            day2 = Label3.Text
        ElseIf (k = 22) Then
            day2 = Label3.Text
        ElseIf (k = 27) Then
            day2 = Label3.Text
        ElseIf (k = 34) Then
            day2 = Label3.Text
        End If
    Next
    For t = 11 To 37
        If (t = 13) Then
            day3 = Label4.Text
        ElseIf (t = 18) Then
            day3 = Label4.Text
        ElseIf (t = 23) Then
            day3 = Label4.Text
        ElseIf (t = 28) Then
            day3 = Label4.Text
        ElseIf (t = 35) Then
            day3 = Label4.Text
        End If
    Next
    For u = 11 To 37
        If (u = 14) Then
            day4 = Label5.Text
        ElseIf (u = 19) Then
            day4 = Label5.Text
        ElseIf (u = 24) Then
            day4 = Label5.Text
        ElseIf (u = 29) Then
            day4 = Label5.Text
        ElseIf (u = 36) Then
            day4 = Label5.Text
        End If
    Next
    For y = 11 To 37
        If (y = 15) Then
            day5 = Label6.Text
        ElseIf (y = 20) Then
            day5 = Label6.Text
        ElseIf (y = 25) Then
            day5 = Label6.Text
        ElseIf (y = 30) Then
            day5 = Label6.Text
        ElseIf (y = 37) Then
            day5 = Label6.Text
        End If
    Next

    Dim sql As String
    Dim sql2 As String
    Dim sql3 As String
    Dim sql4 As String
    Dim sql5 As String

    Dim cmd As OleDbCommand
    Dim cmd2 As OleDbCommand
    Dim cmd3 As OleDbCommand
    Dim cmd4 As OleDbCommand
    Dim cmd5 As OleDbCommand

    Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=scheduledatabase.accdb;Persist Security Info=False;")
    sql = "INSERT INTO consultationschedule([Time], weekDay, Available)VALUES(@time1,@day1,@available1)"
    sql2 = "INSERT INTO consultationschedule([Time], weekDay, Available)VALUES(@time2,@day2,@available2)"
    sql3 = "INSERT INTO consultationschedule([Time], weekDay, Available)VALUES(@time3,@day3,@available3)"
    sql4 = "INSERT INTO consultationschedule([Time], weekDay, Available)VALUES(@time4,@day4,@available4)"
    sql5 = "INSERT INTO consultationschedule([Time], weekDay, Available)VALUES(@time5,@day5,@available5)"

    Using (conn)
        conn.Open()
        cmd = New OleDbCommand(sql, conn)
        cmd.Parameters.Add(New OleDbParameter("@Time", time1))
        cmd.Parameters.Add(New OleDbParameter("@weekDay", day1))
        cmd.Parameters.Add(New OleDbParameter("@Available", available1))
        cmd2 = New OleDbCommand(sql2, conn)
        cmd2.Parameters.Add(New OleDbParameter("@Time", time2))
        cmd2.Parameters.Add(New OleDbParameter("@weekDay", day2))
        cmd2.Parameters.Add(New OleDbParameter("@Available", available2))
        cmd3 = New OleDbCommand(sql3, conn)
        cmd3.Parameters.Add(New OleDbParameter("@Time", time3))
        cmd3.Parameters.Add(New OleDbParameter("@weekDay", day3))
        cmd3.Parameters.Add(New OleDbParameter("@Available", available3))
        cmd4 = New OleDbCommand(sql4, conn)
        cmd4.Parameters.Add(New OleDbParameter("@Time", time4))
        cmd4.Parameters.Add(New OleDbParameter("@weekDay", day4))
        cmd4.Parameters.Add(New OleDbParameter("@Available", available4))
        cmd5 = New OleDbCommand(sql5, conn)
        cmd5.Parameters.Add(New OleDbParameter("@Time", time5))
        cmd5.Parameters.Add(New OleDbParameter("@weekDay", day5))
        cmd5.Parameters.Add(New OleDbParameter("@Available", available5))

        Dim f As Integer = cmd.ExecuteNonQuery()
        If (f >= 1) Then
            ToolStrip1.Text += f.ToString & " record added successfully"
        Else
            ToolStrip1.Text += "Unable to add record"
        End If
    End Using
End Sub

Upvotes: 0

Views: 107

Answers (1)

Wai Fung Au Yeung
Wai Fung Au Yeung

Reputation: 131

I suggest you create a new method for creating a record:

 Private Sub AddRecords(ByVal time As String, ByVal day As String, ByVal available As String)

        Dim sql As String
        Dim cmd As OleDbCommand

        Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=scheduledatabase.accdb;Persist Security Info=False;")
        sql = "INSERT INTO consultationschedule(Time, weekDay, Available)VALUES(@Time,@weekDay,@Available)"

        Using (conn)
            conn.Open
            cmd = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@Time", time)
            cmd.Parameters.AddWithValue("@weekDay", day)
            cmd.Parameters.AddWithValue("@Available", available)
            Dim f As Integer = cmd.ExecuteNonQuery()
            If (f >= 1) Then
                ToolStrip1.Text += f.ToString & " record added successfully"
            Else
                ToolStrip1.Text += "Unable to add record"

            End If
            MessageBox.Show("User Widrawed", "Widrawed", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Using
    End Sub

After that you could call the method for adding records

AddRecords(time1, day1, available1)
AddRecords(time2, day2, available2)
AddRecords(time3, day3, available3)
AddRecords(time4, day4, available4)
AddRecords(time5, day5, available5)

Upvotes: 2

Related Questions