I use the this code. showing "Update" msg but not update and if i add an item then show "Update" not "save"
Please help me.....

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        Dim sql = "select Count(*) from pur_invdet where purinvid= txtpurinvid.text"
        Dim cmd = New OleDb.OleDbCommand(sql, cn)
        cmd.Parameters.AddWithValue("@purinvid", TextBox1.Text)
        Dim result As Integer = Convert.ToInt32(cmd.ExecuteScalar())
        cmd.Parameters.Clear()
        cmd.Dispose()

        If result > 0 Then

            For Each x As ListViewItem In ListView1.Items
                Dim sln As Integer = x.SubItems(0).Text
                Dim qty As Integer = Convert.ToDecimal(x.SubItems(4).Text)
                Dim itmid As Integer = x.SubItems(12).Text
            Next
            sql = "Update pur_invdet set [slno] = [slno] + sln where purinvid= txtpurinvid.text"
            cmd = New OleDb.OleDbCommand(sql, cn)
            cmd.Parameters.AddWithValue("@slno", Val(txtitemsl.Text))
            cmd.Parameters.AddWithValue("@ItemIDpur", txtitemid.Text)
            cmd.Parameters.AddWithValue("@purinvid", txtpurinvid.Text)
            cmd.ExecuteNonQuery()
            MsgBox("Updated")
        Else

            sql = "Insert into pur_invdet (slno, ItemIDpur,purinvid) values (@slno, @ItemIDpur, @purinvid)"
            cmd = New OleDb.OleDbCommand(sql, cn)
            cmd.Parameters.AddWithValue("@slno", Val(txtitemsl.Text))
            cmd.Parameters.AddWithValue("@ItemIDpur", txtitemid.Text)
            cmd.Parameters.AddWithValue("@purinvid", txtpurinvid.Text)
            cmd.ExecuteNonQuery()
            MsgBox("Save")

        End If

Dani AI

Generated

You are getting the "Updated" message because the code never evaluates existence per item, and your UPDATE uses a local variable name (sln) that never becomes a SQL parameter. Also, with OleDb the names of parameters are ignored and values are bound by position, so "@x" vs "@y" does not matter; the order you add them must match the order of the placeholders in the SQL. As suggested, always check the rows affected, and as noted, do the work inside the loop, item by item.

A simple, reliable pattern with OleDb is: for each ListView row try an UPDATE first; if 0 rows were affected, INSERT. Wrap the whole batch in a transaction so it is all-or-nothing.

'Assumes columns: purinvid (key), ItemIDpur (key), slno, qty
Dim tr = cn.BeginTransaction()
Try
    For Each li As ListViewItem In ListView1.Items
        Dim sln As Integer = CInt(li.SubItems(0).Text)
        Dim qty As Decimal = CDec(li.SubItems(4).Text)
        Dim itmid As Integer = CInt(li.SubItems(12).Text)
        Dim purid As String = txtpurinvid.Text

        Using cmdU As New OleDb.OleDbCommand(
            "UPDATE pur_invdet SET slno = slno + ?, qty = qty + ? WHERE purinvid = ? AND ItemIDpur = ?", cn, tr)
            cmdU.Parameters.Add("@p1", OleDb.OleDbType.Integer).Value = sln
            cmdU.Parameters.Add("@p2", OleDb.OleDbType.Decimal).Value = qty
            cmdU.Parameters.Add("@p3", OleDb.OleDbType.VarChar).Value = purid
            cmdU.Parameters.Add("@p4", OleDb.OleDbType.Integer).Value = itmid
            Dim n = cmdU.ExecuteNonQuery()
            If n = 0 Then
                Using cmdI As New OleDb.OleDbCommand(
                    "INSERT INTO pur_invdet(purinvid, ItemIDpur, slno, qty) VALUES (?,?,?,?)", cn, tr)
                    cmdI.Parameters.Add("@p1", OleDb.OleDbType.VarChar).Value = purid
                    cmdI.Parameters.Add("@p2", OleDb.OleDbType.Integer).Value = itmid
                    cmdI.Parameters.Add("@p3", OleDb.OleDbType.Integer).Value = sln
                    cmdI.Parameters.Add("@p4", OleDb.OleDbType.Decimal).Value = qty
                    cmdI.ExecuteNonQuery()
                End Using
            End If
        End Using
    Next
    tr.Commit()
    MessageBox.Show("Done")
Catch ex As Exception
    tr.Rollback()
    MessageBox.Show(ex.Message)
End Try

If this is actually MySQL, switch to MySql.Data.MySqlClient and simplify with a composite unique key on (purinvid, ItemIDpur) and one command:
INSERT INTO pur_invdet(purinvid,ItemIDpur,slno,qty) VALUES(@id,@item,@sl,@qty) ON DUPLICATE KEY UPDATE slno = slno + VALUES(slno), qty = qty + VALUES(qty);

Recommended Answers

All 6 Replies

Change lines #21 and 22 into the following to see how many registers are really updated:

           dim n as int32 = cmd.ExecuteNonQuery() 'n=The number of rows affected'
           MsgBox("Updated "+n.tostring+" rows")

Change lines #30 and 31 into the following and see how many registers are really saved:

           dim n as int32= cmd.ExecuteNonQuery() 'n=The number of rows affected'
           MsgBox("Saved " + n.tostring + " rows")

You are setting just one parameter in the Update statement while adding later 3 parameters. Can't you include the execute cmd.executeNonQuerys inside a try - catch to see if there is any error message?

The code had severall errors. Maybe this other way you can make it work:

        Dim sql = "select Count(*) from pur_invdet where purinvid=@purinvid"
        Dim cmd = New OleDb.OleDbCommand(sql, cn)
        cmd.Parameters.AddWithValue("@purinvid", TextBox1.Text)
        Dim result As Integer = Convert.ToInt32(cmd.ExecuteScalar())
        cmd.Parameters.Clear()

        Try
            If result > 0 Then

                For Each x As ListViewItem In ListView1.Items
                    Dim sln As Integer = x.SubItems(0).Text
                    Dim qty As Integer = Convert.ToDecimal(x.SubItems(4).Text)
                    Dim itmid As Integer = x.SubItems(12).Text
                Next
                sql = "Update pur_invdet set [slno] = @slno, [ItemIDpur]=@ItemIDpur  where purinvid=@purinvid"
                cmd = New OleDb.OleDbCommand(sql, cn)
                cmd.Parameters.AddWithValue("@slno", Val(txtitemsl.Text))
                cmd.Parameters.AddWithValue("@ItemIDpur", txtitemid.Text)
                cmd.Parameters.AddWithValue("@purinvid", txtpurinvid.text)
                Dim n As Int32 = cmd.ExecuteNonQuery()
                If n Then
                    MsgBox("Updated ")
                End If
            Else

                sql = "Insert into pur_invdet (slno, ItemIDpur,purinvid) values (@slno, @ItemIDpur, @purinvid)"
                cmd = New OleDb.OleDbCommand(sql, cn)
                cmd.Parameters.AddWithValue("@slno", Val(txtitemsl.Text))
                cmd.Parameters.AddWithValue("@ItemIDpur", txtitemid.Text)
                cmd.Parameters.AddWithValue("@purinvid", txtpurinvid.text)
                Dim n As Int32 = cmd.ExecuteNonQuery()
                If n Then
                    MsgBox("Saved")
                End If
            End If
            cmd.Dispose()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

@Jayanta_1: In a parameterised sql statement you must have to use the parameter before adding/creating the parameter. In your first sql statement you didn't use the parameter into the statement. use of parameterised sql statement is the best practice to prevent your database from sql injections.
The codes should be as @xrj already discussed i.e.

        Dim sql = "select Count(*) from pur_invdet where purinvid=@purinvid"
        Dim cmd = New OleDb.OleDbCommand(sql, cn)
        cmd.Parameters.AddWithValue("@purinvid", TextBox1.Text)
        Dim result As Integer = Convert.ToInt32(cmd.ExecuteScalar())

But do not understand why did not you do the update and save codes for new items in the for loop to run between the listviewitems and the updates should be run for every items and save for new items. So previous veriable you declared as resulthas no proper functionality to check and do your updates /save.

And I do not understand what you want to do. Please explain briefly what you want to save/update for every listview items or store a cumulative value for a item if there has duplicate value if any.

@Shark purinvid seems to be the table's key, so if it does not exist resultwill be 0 out from executeScalarand the row will be added. In the other case the row will be updated.

Not much of development lately but i think u need to match ur field change to ur recordset and then just replace it with the field value....

For save u just need to match record count and insert all fields.

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.