Hi,

In Vb i have a form for login and many forms.After i login the username is displayed in all the forms. I can add,update and save the records in database table. But i now want to store the Username to these records so that to check who has entered the particular record.

Can anybody tell me how to write the code for saving the Username.

Thanks

Dani AI

Generated

Quick practical guidance to capture which user created or changed a record. asked how to save the username; and pointed toward stamping the record at save time. A maintainable approach is to keep a single user identity in your schema and stamp records when you insert/update so you get created/modified timestamps and a stable reference to the user that performed the action.

Add audit columns to the table (for example CreatedById, CreatedAt, ModifiedById, ModifiedAt) and make the user columns foreign keys to a Users table. When saving from VB, set those fields from a single, trusted source (the application session) and use parameterized commands instead of string concatenation. Example VB.NET pattern:

Using cmd As New System.Data.SqlClient.SqlCommand("INSERT INTO Items (Name, CreatedById, CreatedAt) VALUES (@name, @uid, @now)", conn)
    cmd.Parameters.AddWithValue("@name", txtName.Text)
    cmd.Parameters.AddWithValue("@uid", currentUserId)    ' set once at login
    cmd.Parameters.AddWithValue("@now", DateTime.UtcNow)
    cmd.ExecuteNonQuery()
End Using

Keep the logged-in identity in one place so every form reads the same value: a Module-level public variable, a singleton/session object, or a property on the main form. Validate that identity at login against the Users table so the app uses an authoritative ID rather than a user-supplied string.

Notes and cautions: use transactions if you also insert audit/history rows; index the timestamp/user columns for reporting; prefer storing the user id (and optionally the display name snapshot for human-readable history) so name changes do not break your audit trail; never trust client-supplied text for who performed the action. This builds on the ideas from and with concrete patterns to implement safely.

Recommended Answers

All 2 Replies

use a code (a number ) for each user and put it in every table. Or create the primary key of the table with some relation with the userid.

Krs13,
If you can add and update records in database then I think there should not be any problem with saving username with records. I feel it is if you add one line of code like

rs!Editedby = txtUsername.Text

in your add and update code. Here rs is recordset that you dim.
Thanks

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.