dear all,

i need help about asp.net. i'm making application with login form from visual studio and using loginname in every pages, my questions is:
how to get username online into database when user insert/update? because i wont to know 'who create this data'

best regards,
martin

Dani AI

Generated

As noted, the Visual Studio Login control and the session/cookie approach mentioned by will work, but a reliable "created by" audit needs the server-side authenticated principal at the moment of insert/update. Client-side cookies or hidden fields can be tampered with or may expire; the server-side identity is authoritative.

Recommended pattern (simple, safe):

  1. Confirm the request is authenticated (User.Identity.IsAuthenticated).
  2. Read the server principal (User.Identity.Name) or, preferably, the unique user id from the membership/identity system.
  3. Pass that value as a parameter into the insert/update command (never concatenate into SQL). Prefer storing a stable user id (ProviderUserKey or AspNetUsers.Id) rather than a display name. Record CreatedOnUtc as well for a complete audit trail.

Example (Web Forms / classic ADO.NET):

string createdBy = (User != null && User.Identity.IsAuthenticated) ? User.Identity.Name : "anonymous";

using (var cn = new SqlConnection(connString))
using (var cmd = new SqlCommand("INSERT INTO Items (Title, CreatedBy, CreatedOnUtc) VALUES (@title, @createdBy, @createdOn)", cn))
{
    cmd.Parameters.Add("@title", SqlDbType.NVarChar, 200).Value = txtTitle.Text;
    cmd.Parameters.Add("@createdBy", SqlDbType.NVarChar, 256).Value = createdBy;
    cmd.Parameters.Add("@createdOn", SqlDbType.DateTime).Value = DateTime.UtcNow;
    cn.Open();
    cmd.ExecuteNonQuery();
}

Troubleshooting and best practices: if createdBy is empty, common causes are unauthenticated requests, session timeout, or missing/invalid auth cookie. In web-farm scenarios ensure consistent machineKey so auth cookies decrypt. Always use parameterized queries or stored procedures to prevent SQL injection. For robust auditing, store CreatedById (user id), CreatedOnUtc, and LastModifiedById/LastModifiedOnUtc.

Recommended Answers

All 7 Replies

Create Session variable or Hidden control and assign username into session vairable or hidden control when the login is succssfull. Using session variable u can know who is inserting the data or updating

Thanks..
by the way.. do you know about ebout ebook or link to session asp.net?

best regards,
martin

Search in net u will get idea about the session

Thanks for your help...
i need help again... after using cookies...
but i don't understand how to get username into database...

maybe some code....

if someone insert/update his name can find in database 'create by'

Once the login is successful u can came to know the username as 'Create by', that assign into session or cookie variable. When user is try to insert/update the record, pass this username into backend to update 'Create by' using the Insert/update query.

ok, i understand
when user push insert/update button, he send his cookies

my program is working now

best regards,
martin

Fine. Then mark as resovled

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.