I have trouble updating my database. When I run my program and click on update database, I can then choose a record in my listbox, but when I click on save it button it goes to "Geen leidraad is Bygevoeg nie"

Here is my code (button update)

Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click

        'Edit Button to to edit my data in database

        StatusLabel.Text = " Kies Leidraad om te verander"

        If ListBox1.SelectedIndex <> -1 Then

            SaveOrEdit = "Edit"
        Else
            StatusLabel.Text = " Geen bestaande leidraad is geselekteer nie, kies nou een om te verander"
        End If

           End Sub

My save button code

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        'button to save and edit databse

        If SaveOrEdit = "Save" Then

            Dim add As DialogResult

            add = Cls_MessageBbox.Show("Is jy Seker jy will Leidraad" & vbCrLf & vbCrLf & TxtLuidraad.Text & vbCrLf & vbCrLf & "Byvoeg", "Byvoeg.", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

            If add = Windows.Forms.DialogResult.Yes Then
                AddNew()
            Else
                 StatusLabel.Text = "Geen leidraad is Bygevoeg nie"
            End If

        ElseIf SaveOrEdit = "Edit" Then

            Edit()
        Else

            StatusLabel.Text = " Geen Leidraad is bygevoeg"

        End If


    End Sub

My update function

Public Function Edit() As String

        'Function to edit my data

        Try

            Dim OleDbConn As OleDbConnection = New OleDbConnection(ConnString)
            OleDbConn.Open()

            Dim MyOledbCommand As OleDbCommand = New OleDbCommand()

            Dim String1, String2, String3, String4, String5, string6, string7, string8, string9, string10, string11, string12, string13 As String

            String1 = TxtLuidraad.Text
            String2 = txtA1.Text()
            String3 = txtA2.Text()
            String4 = txtA3.Text()
            String5 = txtA4.Text()
            string6 = txtA5.Text()
            string7 = txtA6.Text()
            string8 = txtA7.Text()
            string9 = txtA8.Text()
            string10 = txtA9.Text()
            string11 = txtA10.Text()
            string12 = txtA11.Text()
            string13 = txtA12.Text()

            MyOledbCommand.CommandText = "UPDATE Tblokkies SET A1 = @string2, A2 = @string3, A3 = @string4, A4 = @string5, A5 = @string6, A6 = @string7, A7 = @string8, A8 = @string9, A9 = @string10, A10 = @string11, A11 = @string12, A12 = @string13  WHERE leidraad = @string1"

            MyOledbCommand.Parameters.AddWithValue("@string1", Me.TxtLuidraad.Text)
            MyOledbCommand.Parameters.AddWithValue("@string2", Me.txtA1.Text)
            MyOledbCommand.Parameters.AddWithValue("@string3", Me.txtA2.Text)
            MyOledbCommand.Parameters.AddWithValue("@string4", Me.txtA3.Text)
            MyOledbCommand.Parameters.AddWithValue("@string5", Me.txtA4.Text)
            MyOledbCommand.Parameters.AddWithValue("@string6", Me.txtA5.Text)
            MyOledbCommand.Parameters.AddWithValue("@string7", Me.txtA6.Text)
            MyOledbCommand.Parameters.AddWithValue("@string8", Me.txtA7.Text)
            MyOledbCommand.Parameters.AddWithValue("@string9", Me.txtA8.Text)
            MyOledbCommand.Parameters.AddWithValue("@string10", Me.txtA9.Text)
            MyOledbCommand.Parameters.AddWithValue("@string11", Me.txtA10.Text)
            MyOledbCommand.Parameters.AddWithValue("@string12", Me.txtA11.Text)
            MyOledbCommand.Parameters.AddWithValue("@string13", Me.txtA12.Text)


            MyOledbCommand.Connection = OleDbConn

            MyOledbCommand.ExecuteNonQuery()
            OleDbConn.Close()

            DisableTextboxes()
            NeroBar1.Value = 0

            FillDataGrid("Select * from Tblokkies")
            FillListBox("Select * from Tblokkies")

            btnaddNew.Enabled = True
            btnDelete.Enabled = True
            ' btnupdate.Enabled = True

            SaveOrEdit = "Cancel"

            'StatusLabel.Text = " Inligtin Verander."
            StatusLabel.Text = "Leidraad   :" & TxtLuidraad.Text & ": Verander."

        Catch err As System.Exception
            StatusLabel.Text = err.Message
        End Try
    End Function

With a quick look I couldn't spot any error (in Edit function).

I suggest setting a breakpoint to line MyOledbCommand.ExecuteNonQuery() , and before executing the command, check the properties of MyOledbCommand especially MyOledbCommand.CommandText.

teme64

Thanks I have put a breakpoint at my function. It looks like it don't get to my function, so the problem is by my button save, and ?I don't know what it is.

Ok, now I checked those button handlers and your original question.

First, have you declared SaveOrEdit variable as a string?

Not sure about your program's logic, but from the code can be seen that SaveOrEdit gets never a value "Save" (unless that's the initial value or you set it somewhere else not shown above).

If the first event is btnupdate_Click and you have made a selection from the listbox, then SaveOrEdit = "Edit". Next, if btnAdd_Click is called, program calls Edit() function and sets SaveOrEdit = "Cancel".

I suggest that you put a breakpoint in to each place where you either check the value of SaveOrEdit or assign it with a new value. Then step through your app (F8-key steps the code) and try to spot the 'logical' error. By 'logical' error I mean, the code obviously doesn't execute in the way you designed to do.

Also, what is Cls_MessageBbox? Do you have Dim Cls_MessageBbox As MessageBox or have you created your own messagebox? If the latter is true, then check add = Cls_MessageBbox.Show("... . That's the point where you end up to "Geen leidraad is Bygevoeg nie".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.