Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to update (bv.net 2008 express)

Status
Not open for further replies.

hendrikbez

Technical User
Nov 3, 2003
54
ZA
I am not getting my program to update my access 2003 database. What the hek am I doing wrong.

This is the only problem that I still have in my program

Hope someone here can help me out!

Here is my code (function)

Code:
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")

            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

Here is my buton edit code
Code:
    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

Here is my button save code

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
                ListBox1.Text = "Geen leidraad is Bygevoeg nie"

            End If

        ElseIf SaveOrEdit = "Edit" Then

            Edit()
        Else

            StatusLabel.Text = " Geen Leidraad is bygevoeg"

        End If


    End Sub


How will you spend eternity - Smoking or Non Smoking?
 
>>This is the only problem that I still have in my program
What is the problem??

Zameer Abdulla
 
I can not update my data in my databse

How will you spend eternity - Smoking or Non Smoking?
 
No there is no error

How will you spend eternity - Smoking or Non Smoking?
 
Since the rest of the code I am unable to understand because it is not a know language for me, I will post a working code.

Code:
    Function UpdateData() As Boolean
        Try
            Dim OleDbConn As OleDbConnection = New OleDbConnection(GetConnectionString())


            Dim strSQL As String = "UPDATE Employees SET City=@City , Region=@Region" _
                            & " WHERE EmployeeId=@EmployeeId"

            Dim MyOledbCommand As OleDbCommand = New OleDbCommand(strSQL, OleDbConn)


            MyOledbCommand.Parameters.AddWithValue("@City", Me.TextBox1.Text)
            MyOledbCommand.Parameters.AddWithValue("@Region", Me.TextBox2.Text)
            MyOledbCommand.Parameters.AddWithValue("@EmployeeId", Me.TextBox3.Text)

            OleDbConn.Open()
            MyOledbCommand.ExecuteNonQuery()
            Return True
            OleDbConn.Close()

        Catch ex As Exception
            Return False
            MessageBox.Show(ex.Message)
        End Try

    End Function

GetConnectionString() is function that returns the the connectionstring from a module.

Zameer Abdulla
 
Thanks for the code, but I think my problem is with my update or save button

How will you spend eternity - Smoking or Non Smoking?
 
Also the difference between
Code:
Dim MyOledbCommand As OleDbCommand = New OleDbCommand(strSQL, OleDbConn)
Code:
Dim MyOledbCommand As OleDbCommand = New OleDbCommand()

and the order of lines


Zameer Abdulla
 
I have changed my code to this that you have showing me.

I click on update then I select a item in my listbox to update, I put the new info in one of my textboxes, and then I click on save, then is said no info was save, I think the prob is with the save, but I cannot fig it out

Code:
 Function Edit() As Boolean
        Try
            Dim OleDbConn As OleDbConnection = New OleDbConnection(ConnString)
         
            Dim strsql As String = "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"

            Dim MyOledbCommand As OleDbCommand = New OleDbCommand(strsql, OleDbConn)

            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)

            OleDbConn.Open()
            MyOledbCommand.ExecuteNonQuery()
            Return True
            OleDbConn.Close()

        Catch ex As Exception
            Return False
            MessageBox.Show(ex.Message)
        End Try

    End Function

How will you spend eternity - Smoking or Non Smoking?
 
Try this function with a new command button without any clause.

Zameer Abdulla
 
BTW, How do you call the update function?
btnupdate don't do any updation except setting the value
Code:
SaveOrEdit = "Edit"

and the Add Button code
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
                [COLOR=red]ListBox1.[/color]Text = "Geen leidraad is Bygevoeg nie"

            End If
        ElseIf SaveOrEdit = "Edit" Then
            Edit()
        Else
            StatusLabel.Text = " Geen Leidraad is bygevoeg"
        End If
    End Sub
Is this correct?

Zameer Abdulla
 
Code:
ListBox1.Text = "Geen leidraad is Bygevoeg nie"
I did change it to
Code:
StatusLabel.Text = " Geen Leidraad is bygevoeg"
I thouth that this will use the function update
Code:
 ElseIf SaveOrEdit = "Edit" Then
[COLOR=red]Edit()[/color]
        Else
            StatusLabel.Text = " Geen Leidraad is bygevoeg"
        End If
to call function
Code:
Public Function Edit() As String
Now I am more confused about calling the edit/update function



How will you spend eternity - Smoking or Non Smoking?
 
I will change the edit function return to Boolean instead of String
Code:
    Public Function Edit() As Boolean
        'code here..
    End Function
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
                ListBox1.Text = "Geen leidraad is Bygevoeg nie"

            End If
        ElseIf SaveOrEdit = "Edit" Then
[COLOR=red]
            If Edit() = True Then
                MessageBox.Show("Updation success")
            Else
                'the error message to show
            End If[/color]
        Else
            StatusLabel.Text = " Geen Leidraad is bygevoeg"
        End If
    End Sub

Zameer Abdulla
 
I have changed the code like this, but it still does not go to edit fucntion.

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

        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
                ListBox1.Text = "Geen leidraad is Bygevoeg nie"
            End If
        ElseIf SaveOrEdit = "Edit" Then

            If Edit() = True Then
                MessageBox.Show("Leidraad is verander")
            Else
                StatusLabel.Text = " Die Leidraad is nie verander nie"
            End If
        Else
            StatusLabel.Text = " Geen Leidraad is bygevoeg"
        End If
    End Sub

When I put breakpoint on
Code:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

it jumb like this

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

 If SaveOrEdit = "Save" Then

 ElseIf SaveOrEdit = "Edit" Then

 Else
            StatusLabel.Text = " Geen Leidraad is bygevoeg"
        End If

It does not go to fucntion



How will you spend eternity - Smoking or Non Smoking?
 
Did you check what was the value of "SaveOrEdit" when you were at the breakpoint.

Zameer Abdulla
 
on
Code:
If SaveOrEdit = "save" then
it shows SaveOrEdit ""

on
Code:
Elseif SaveOrEdit = "Edit" then
it shows SaveOrEdit ""

I don't know why

How will you spend eternity - Smoking or Non Smoking?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top