Problem updating data from a datagrid
I have a data grid that displays data from the DB. Noe i want the user to be able update data in the datadrid cell and update the records.How do i do that? NB: the update should only update the cells whose content has change.
here is my code that displays data in the grid
Private Sub PopulateGrid()
Dim ObjConn As New SqlConnection("server=(local);database=NHIF;Integr ated security=true")
Dim objDataAdapter As New SqlDataAdapter("select * from members", ObjConn)
'Try
objDataAdapter.SelectCommand = New SqlCommand
objDataAdapter.SelectCommand.Connection = ObjConn
objDataAdapter.SelectCommand.CommandText = "select DFname,DLname,Gender,MembershipNo,IssuedDate,Typeo fCard,BirthDate from currentID where CHECKNO='" & Trim(txtCheckNo.Text) & "'"
objDataAdapter.SelectCommand.CommandType = CommandType.Text
ObjConn.Open()
objDataAdapter.Fill(objDataset, "currentID")
ObjConn.Close()
DataGridView1.DataSource = objDataset
DataGridView1.DataMember = "currentID"
' Catch
'MessageBox.Show(Err.Description)
'End Try
objDataAdapter = Nothing
ObjConn = Nothing
End Sub
Also i have a number of textbox control(about 10) that displays data from the db. I want to allow users to update records in the textbox. Currently it works fine but what i wanted to do is to just update the textbox whose data has change and not all of them where they have changed or not. how do i do that? here is the code that currently update some of the data from the textbox
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
' On Error Resume Next
Dim objCommand As SqlCommand = New SqlCommand()
'Dim objconn As New SqlCommand
Dim ObjConn As New SqlConnection("server=(local);database=NHIF;Integr ated security=true")
Dim objDataAdapter As New SqlDataAdapter("select * from members", ObjConn)
'Dim objDataView As DataView()
objDataView = New DataView(objDataset.Tables("members"))
objCommand.Connection = ObjConn
objCommand.CommandText = "UPDATE MEMBERS SET Status= @Status, BirthDT= @BirthDT where CheckNo=@Checkno"
objCommand.CommandType = CommandType.Text
objCommand.Parameters.AddWithValue("@Status", cboStatus.Text)
objCommand.Parameters.AddWithValue("@BirthDT", txtBIRTHDT.Text)
objCommand.Parameters.AddWithValue("@CheckNo", BindingContext(objDataView).Current("CheckNo"))
'objCommand.Parameters.AddWithValue("@CheckNo", txtCheckNo.Text)
ObjConn.Open()
objCommand.ExecuteNonQuery()
FillDataSetAndView()
BindControls()
MessageBox.Show("Records Updated")
End Sub
|