0

i have no problem when inserting records if it has a duplicate, but not on updating a record, and i really have no idea what to do here.

here's my code:

Imports MySql.Data.MySqlClient
Public Class Form3
Public id As Integer
Public sConnection As New MySqlConnection
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If sConnection.State = ConnectionState.Closed Then
        sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
        sConnection.Open()
    End If
    LoadPeople()

End Sub
Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
    If id = Nothing Then
        MsgBox("Please choose a record to edit.", MsgBoxStyle.Exclamation)
    Else
        Dim sqlQuery As String = "SELECT * FROM candidate WHERE cid = '" & lvPeople.SelectedItems(0).Text & "'"
        Dim sqlAdapter As New MySqlDataAdapter
        Dim sqlCommand As New MySqlCommand
        Dim sqlTabble As New DataTable

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = sConnection
            .ExecuteNonQuery()
        End With

        With sqlAdapter
            .SelectCommand = sqlCommand
            .Fill(sqlTabble)
        End With
        Form5.id = lvPeople.SelectedItems(0).Text
        Form5.cfname = sqlTabble.Rows(0)("cfname")
        Form5.cmname = sqlTabble.Rows(0)("cmname")
        Form5.clname = sqlTabble.Rows(0)("clname")
        Form5.cpos = sqlTabble.Rows(0)("cpos")
        Form5.cyr = sqlTabble.Rows(0)("cyr")
        Form5.cparty = sqlTabble.Rows(0)("cparty")
        Form5.vyear = sqlTabble.Rows(0)("vyear")
        Form5.ShowDialog()
        id = Nothing
    End If



End Sub

Private Sub lvPeople_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvPeople.MouseClick
    id = lvPeople.SelectedItems(0).Selected
End Sub

my Listview code:

Public Sub LoadPeople()
    Dim sqlQuery As String = "select * from candidate where cfname<>'Select a Candidate'"
    Dim sqlAdapter As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim TABLE As New DataTable
    Dim i As Integer

    With sqlCommand
        .CommandText = sqlQuery
        .Connection = sConnection
    End With

    With sqlAdapter
        .SelectCommand = sqlCommand
        .Fill(TABLE)
    End With

    lvPeople.Items.Clear()

    For i = 0 To TABLE.Rows.Count - 1
        With lvPeople
            .Items.Add(TABLE.Rows(i)("cid"))
            With .Items(.Items.Count - 1).SubItems
                .Add(AddFieldValue(TABLE.Rows(i), ("cpos")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cfname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cmname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("clname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cyr")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cparty")))
                .Add(AddFieldValue(TABLE.Rows(i), ("vyear")))
            End With
        End With
    Next

End Sub

Private Function AddFieldValue(ByVal row As DataRow, ByVal fieldName As String) As String
    If Not DBNull.Value.Equals(row.Item(fieldName)) Then
        Return CStr(row.Item(fieldName))
    Else
        Return Nothing
    End If
End Function

this update form will popup if the edit button is pressed

Imports MySql.Data.MySqlClient
Public Class Form5
Friend id As Integer
Friend cfname As String
Friend clname As String
Friend cmname As String
Friend cpos As String
Friend cyr As String
Friend cparty As String
Friend vyear As Integer

Public sConnection As New MySqlConnection

Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If sConnection.State = ConnectionState.Closed Then
        sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
        sConnection.Open()
    End If

    txtfname.Text = cfname
    txtlname.Text = clname
    txtmname.Text = cmname
    cmbpos.Text = cpos
    cmbyr.Text = cyr
    txtparty.Text = cparty
    TextBox1.Text = vyear

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim conn As New MySqlConnection
    Dim cmd As New MySqlCommand
    Dim dr As MySqlDataReader

    conn.ConnectionString = "server = localhost; user id = root; database = db; password = root"
    cmd.Connection = conn
    conn.Open()
    cmd.CommandText = " SELECT * FROM candidate WHERE cfname LIKE '" & txtfname.Text & "' AND cmname='" & txtmname.Text & "' AND clname='" & txtlname.Text & "'"
    dr = cmd.ExecuteReader

    If dr.HasRows Then
        MessageBox.Show("Entry already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    ElseIf txtfname.Text = "" Or txtlname.Text = "" Or txtmname.Text = "" Or txtparty.Text = "" Or cmbyr.Text = "" Or cmbpos.Text = "" Then
        MessageBox.Show("Please complete the required fields..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        Dim sqlQuery As String = "UPDATE candidate SET cfname='" & txtfname.Text & "', cmname='" & txtmname.Text & "', clname='" & txtlname.Text & "' , cpos='" & cmbpos.Text & "', cyr='" & cmbyr.Text & "', cparty='" & txtparty.Text & "' WHERE cid='" & id & "'"
        Dim sqlCommand As New MySqlCommand

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = sConnection
            .ExecuteNonQuery()
        End With
        MsgBox("Record Updated")
        Dispose()
    End If


    Form3.LoadPeople()
    Me.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Hide()
End Sub
End Class

i tried to delete the record when i press the edit button so it will not find the entry then insert the edited entry when i press update button and insert the same entry when i press the cancel button, and yeah it didn't work.

Sky Scraper
  • 185
  • 2
  • 5
  • 20
  • 1
    Not related to your *question* but **very** related to you code: [How do I create a parameterized SQL query? Why Should I?](http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i) – Bjørn-Roger Kringsjå Sep 05 '14 at 05:44
  • Argh! Why does everyone want to abuse `ListViews`? Do it properly. Populate a DataTable and bind that to a `DataGridView` via a BindingSource. All changes made in the grid are automatically pushed to the table and any changes made in code should be made via the `BindingSource`. Saving is then a two-line affair: call `EndEdit` on the `BindingSource` and `Update` on the same data adapter that retrieved the data in the first place. Simples! – jmcilhinney Sep 05 '14 at 08:35

0 Answers0