how do i update a record in a table after i populated intxtBoxex.text

sudani1969

Member
Joined
Sep 25, 2009
Messages
13
Programming Experience
Beginner
hello every one i wounder if i can have some tip on how to update a single at a time after a serch for it using vb code
beloo is my search code and after it is
Trim(UCase(txt_Search.Text))
Try
If txt_Search.Text = "" Then

MsgBox("Please enter search data")
txt_Search.Focus()
Else

If rb_searchFirstname.Checked = True Then
MainDA.SelectCommand.CommandText = "SELECT * FROM StDetails WHERE [First_Name] = '" & txt_Search.Text & "'"

ElseIf rb_searchSurname.Checked = True Then
MainDA.SelectCommand.CommandText = "SELECT * FROM StDetails WHERE Last_Name = '" & txt_Search.Text & "'"

ElseIf rbID.Checked = True Then
Try
MainDA.SelectCommand.CommandText = "SELECT * FROM StDetails WHERE Civil_ID = '" & (Trim(txt_Search.Text)) & "'"
Catch
MsgBox("Please Enter an Integer Number for ID", MsgBoxStyle.Critical)
txt_Search.Clear()
txt_Search.Focus()
Exit Sub
End Try
End If

MainDA.SelectCommand.ExecuteNonQuery()
DataSet1.Clear()
MainDA.Fill(DataSet1)
grp_Results.Enabled = True
grbUpdate.Enabled = False
Display(DataSet1, "First")


End If

Catch oleDbexceptionParameter As OleDb.OleDbException
Console.WriteLine(oleDbexceptionParameter.StackTrace)

End Try
End Sub


then i created an update command to update "Middle_Name" as follows
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet
Dim co As New OleDb.OleDbConnection

' Static Dim inc As Int32
Dim sql1 As String

Try
' inc = 0
Dim table As DataTable
Dim da As OleDb.OleDbDataAdapter

sql1 = "update StDetails set Middle_Name where Middle_Name = " & txtId.Text & " "
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "StDetails")
table = ds.Tables(0)
Dim cb As New OleDb.OleDbCommandBuilder(da)
table.Rows(0).Item(1) = txtMiddle.Text

da.Update(ds, "StDetails")

MsgBox("Data updated")

Catch oleDbexceptionParameter As OleDb.OleDbException
Console.WriteLine(oleDbexceptionParameter.StackTrace)

End Try

End Sub
 
You should check out this for examples of the most common ADO.NET scenarios:

Retrieving and Saving Data in Databases

You should then check out this for specific information on using parameters in ADO.NET, which you should do every time in preference to using string concatenation, i.e. for both your SELECT and UPDATE statements:

Using Parameters in ADO.NET
 
Back
Top