Note that there are some explanatory texts on larger screens.

plurals
  1. POcshtml file - Locate current windows user, then query database
    text
    copied!<p>I am working on a localhost site that queries, and updates a database. I have not worked with ASP.NET very much.</p> <p>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.</p> <p>I know how to do this with vbscript, but I need to make it work in a cshtml file.</p> <ol> <li>How do I locate the user?</li> <li>What would my select sentence look like?</li> </ol> <p>Any help or suggestions are appreciated.</p> <p>Thanks.</p> <p>Edit:</p> <p>Here is the code that I use to display data from a specific user:</p> <pre><code>@{ 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); } </code></pre> <p>When I change <code>WHERE UserID = 'asmith'</code> to <code>WHERE UserID = @Environment.UserName</code>, I receive the error:</p> <p><code>There was an error parsing the query. [ Token line number = 1,Token line offset = 48,Token in error = . ]</code> and the below is highlighted in red.</p> <pre><code>Line 15: var selectedData = db.Query(selectCommand, searchTerm); </code></pre> <p>Edit #2:</p> <p>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.</p> <pre><code>var CurrUser = Environment.UserName; var db = Database.Open("Database") ; var selectCommand = "SELECT * FROM Table WHERE UserID = @0"; var searchTerm = @CurrUser; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload