I am working on a localhost site that queries, and updates a database. I have not worked with ASP.NET very much.
I am wondering if it would be possible to find out the current windows user, then select all records in the database that match the located username.
I know how to do this with vbscript, but I need to make it work in a cshtml file.
- How do I locate the user?
- What would my select sentence look like?
Any help or suggestions are appreciated.
Thanks.
Edit:
Here is the code that I use to display data from a specific user:
@{
var db = Database.Open("Database") ;
var selectCommand = "SELECT * FROM Table WHERE UserID = 'asmith'";
var searchTerm = "";
var selectedData = db.Query(selectCommand, searchTerm);
var grid = new WebGrid(source: selectedData, defaultSort: "Team", rowsPerPage:20);
}
When I change WHERE UserID = 'asmith'
to WHERE UserID = @Environment.UserName
, I receive the error:
There was an error parsing the query. [ Token line number = 1,Token line offset = 48,Token in error = . ]
and the below is highlighted in red.
Line 15: var selectedData = db.Query(selectCommand, searchTerm);
Edit #2:
This Successfully queries the database and returns the correct data, but isn't clean and is causing an issue with another query on the page.
var CurrUser = Environment.UserName;
var db = Database.Open("Database") ;
var selectCommand = "SELECT * FROM Table WHERE UserID = @0";
var searchTerm = @CurrUser;