none
Update SQL Database with VB.Net RRS feed

  • Question

  • I'm trying to update a single selected record in Data grid. But its update all the record with same value.

    may I know what's wrong with my codes?

    Private Sub updatesearchresBtn_Click(sender As Object, e As EventArgs) Handles updatesearchresBtn.Click
    
            Try
    
                MysqlConnection = New MySqlConnection(StrCon)
                MysqlConnection.Open()
                Dim str As String = "Update tb_userinfo.asset_data set id=@New_id,product=@New_product,entitlement_end_date=@New_eed"
    
                Cmd = New MySqlCommand(str, MysqlConnection)
    
                With Cmd.Parameters
                    .AddWithValue("@New_id", txtSRid.Text)
                    .AddWithValue("@New_product", txtSRproduct.Text)
                    .AddWithValue("@New_eed", txtSREedate.Text.ToString)
                    
                End With
    
    
                Cmd.ExecuteNonQuery()
    
                loadData()
                Cmd.Dispose()
                MysqlConnection.Close()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub

    Thanks a lot

    Wednesday, July 4, 2018 3:55 AM

Answers

  • Your update query has no where clause that would limit it to a single record, currently your code will simply update the 'id', 'product', and 'entitlement_end_date' value for every record in the table.

    Add a where clause to limit what gets updated...

    MySql Update


    Hire Me For This Job!
    Don't forget to vote for Helpful Posts and Mark Answers!
    *This post does not reflect the opinion of Microsoft, or its employees.




    Wednesday, July 4, 2018 3:57 AM
    Moderator

All replies

  • Your update query has no where clause that would limit it to a single record, currently your code will simply update the 'id', 'product', and 'entitlement_end_date' value for every record in the table.

    Add a where clause to limit what gets updated...

    MySql Update


    Hire Me For This Job!
    Don't forget to vote for Helpful Posts and Mark Answers!
    *This post does not reflect the opinion of Microsoft, or its employees.




    Wednesday, July 4, 2018 3:57 AM
    Moderator
  • Hi Prabaharan 

    I agree with Paul,you should modify the code:

     Dim str As String = "Update tb_userinfo.asset_data set id=@New_id,product=@New_product,entitlement_end_date=@New_eed where ..."

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 4, 2018 5:12 AM
  • Got It. Thanks a Lot
    Monday, July 9, 2018 12:55 AM
  • Got It. Thanks a Lot
    Monday, July 9, 2018 12:55 AM