jas S

jas S

  • NA
  • 1
  • 1.7k

Hi! I am Not able to Update database through Datagridview in VB.net Pls help

Nov 13 2012 9:02 AM
Hi! I'm trying to Update the database using Datagridview in VB.net. When modify name, address, phone or mobile number and click the update button, other rows(ID) value also changed and same. Pls help me.... thanks in advance  

My code is here:-

Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim str As String
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If
        If Me.txtID.Text & "" = "" Then
            cmd.Connection = cnn
            cmd.CommandText = "INSERT INTO CLIENT_REGISTER(CLIENTID,NAME,ADDRESS,PHONE,MOBILE) VALUES ('" & Me.txtID.Text & "','" & Me.txtName.Text & "','" & Me.txtAddress.Text & "','" & Me.txtPhone.Text & "','" & Me.txtMobile.Text & "')"
            cmd.ExecuteNonQuery()
        End If
        cmd.Connection = cnn
        str = "UPDATE CLIENT_REGISTER SET NAME='" & Me.txtName.Text & "',ADDRESS='" & Me.txtAddress.Text & "',PHONE='" & Me.txtPhone.Text & "',MOBILE='" & Me.txtMobile.Text & "' WHERE CLIENTID=" & Me.txtID.ReadOnly
        cmd = New OleDbCommand(str, cnn)
        cmd.ExecuteNonQuery()
        RefreshData()
        cnn.Close()
        VEnable()
        Me.btnSave.Enabled = False
        Me.btnEdit.Enabled = False
        Me.btnUpdate.Enabled = False
        Me.btnCancel.Enabled = True
        Me.btnNew.Enabled = True
End Sub

Answers (1)