hey everyone im sort off new on .net and i was wondering if u can help me bigtime.. i am having problems with editing records when i do a search. example i was to search a specific record but then when i try to edit it, it turned out that i am editing the wrong record. but i can update records if i didnt search it.

here is my code for searching:

Private Sub search()
Dim str As String
str = "select * from user_t where Username like ('" & Trim(txtSearch.Text) & "%')"
Try
con.Open()
Dim com1 As New SqlClient.SqlCommand(str, con)
Dim dr As SqlClient.SqlDataReader = com1.ExecuteReader

While dr.Read
txtUsername.Text = "" & (dr.GetValue(0))
txtPassword.Text = "" & (dr.GetValue(1))
cbUsertype.Text = "" & (dr.GetValue(2))

End While
If Not dr.HasRows Then
MsgBox("No records found. Please try again.", MessageBoxIcon.Error, "No records found")
txtSearch.Clear()
End If
con.Close()
Catch ex As Exception
con.Close()
End Try
End Sub


and for updating:

Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
ds.Tables("user_t").Rows(inc).Item("Username") = txtUsername.Text
ds.Tables("user_t").Rows(inc).Item("Password") = txtPassword.Text
MsgBox("User Updated", MsgBoxStyle.Information, "Update Successful")
da.Update(ds, "user_t")

i really need ur help guys..hehe

Dani AI

Generated

A few practical points that address why an edit after a search can end up changing the wrong row (and how to fix it), building on and 's comments.

A common root cause is mixing a forward-only reader or a text-box population routine with a separate DataSet/DataTable update routine and losing the link to the actual row you intend to edit. If your search returns multiple rows, or if your UI copies values into controls but you never record the record's primary key (or reset the DataTable index), the update call that uses a numeric index (for example Rows(inc)) can point at the wrong row. Always identify the row to edit by a stable key (UserID) instead of relying on position.

A reliable pattern:

  • Query for the specific record using a parameterized WHERE clause (never concatenate user input).
  • Fill a DataTable (or the DataSet) and set its PrimaryKey to the ID column.
  • Locate the DataRow with Rows.Find(id) or Select("UserID = ..."), update the row fields, then call DataAdapter.Update(...).
  • Use SqlCommandBuilder only if you want autogenerated commands; otherwise create explicit UpdateCommand/InsertCommand/DeleteCommand.

Example sketch (VB.NET style) — locate and update by key:

' Fill a DataTable with a parameterized select for the single record,
' set dt.PrimaryKey to the ID column, then:
Dim row As DataRow = dt.Rows.Find(userId)
If row IsNot Nothing Then
  row("Username") = txtUsername.Text
  row("Password") = HashPassword(txtPassword.Text) ' do not store plain text
  Dim cb As New SqlCommandBuilder(da)
  da.Update(dt)
End If

Other tips and checks:

  • Use data binding (BindingSource) so the current record tracks automatically.
  • Avoid LIKE search when you need a single unique record; use exact key lookup or a controlled filter.
  • Dispose connections/commands with Using.
  • Do not store passwords in plain text — use a strong hashing algorithm (PBKDF2/BCrypt/Argon2).
  • If updates still hit the wrong record, add debugging: show the primary key you read on search and the primary key you pass into the update routine to confirm they match.

These steps will make the connection between the searched record and the dataset update deterministic and safe.

Recommended Answers

All 5 Replies

You using a DataReader in this coding; hence you can only read the values not write back to the database.... You need to spend some time looking through some tutorials about working with datasets

hi tom thanks, are there any sites that can tutor me on working with datasets?

It's a MSDN online page - ADO.NET.

SUMMARY:
ADO.NET is a data-access technology that enables applications to connect to data stores and manipulate data contained in them in various ways. It is based on the .NET Framework and it is highly integrated with the rest of the Framework class library. The ADO.NET API is designed so it can be used from all programming languages that target the .NET Framework, such as Visual Basic, C#, J# and Visual C++.

hi tom thanks, are there any sites that can tutor me on working with datasets?

Yes there are many resources & examples available on the internet. If I can recommend one particular book that will fully explain the many different ways of programming with VB & databases it would be Pro ADO.NET 2.0 I think there may be a downloadable version of this book available too if you search for it.

hey everyone, tnx for helping me out... i already finished ds tnx to ur help guys..

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.