Solved

Only show records based on the user that has logged in (User.Identity.Name)

Posted on 2009-07-15
3
235 Views
Last Modified: 2013-11-26
I have a webap using VS2005 and the DB is SQL2005.  I have a webform with a formview and gridview control that allows me to enter records and I use the following two sections of code to capture the username of the individual logged in when I am addeding or updating a existing record using a basic SQLdatasource that select all fields in atable.

Problem: WHen I login to the application to edit the records I worked on using the gridview or fromview control how do I configure the SQLdatasource to only populate the formview or gridviewcontrol with only the records that match the name of the loged in user?  I have tried  "SELECT * FROM TABLE WHERE Username = @Username" and no luck.


Thank you,

 




Protected Sub FormView1_ItemInserting(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting 
 
sqldsCategory.InsertParameters( "UserName").DefaultValue = User.Identity.Name
 
End Sub 
 
Protected Sub ASPxGridView1_RowUpdating(ByVal sender As System.Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs) Handles ASPxGridView1.RowUpdating 
 
sqldsCategory.UpdateParameters("RecordModifier").DefaultValue = User.Identity.Name
 
End Sub

Open in new window

0
Comment
Question by:cesemj
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24862831
Add SelectParamter to your SqlDataSource and use onSelecting Event as below:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
            SelectCommand="SELECT * FROM [yourTable] where UserName=@UserName"
            onselecting="SqlDataSource1_Selecting">
            <SelectParameters>
                <asp:Parameter Name="UserName" />
            </SelectParameters>            
        </asp:SqlDataSource>

Add value to this parameter in codebehind:

Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As SqlDataSourceSelectingEventArgs)
    e.Command.Parameters("@UserName").Value = User.Identity.Name
End Sub
0
 

Author Comment

by:cesemj
ID: 24862881
I will try it and let you know what happens.
0
 

Author Comment

by:cesemj
ID: 24863053
Excellent insight.  Thank you again...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Set value of dynamically created checkboxlist 2 34
Batch file or script with password 22 44
C# Gridview 1 33
Powershell File Sort 8 31
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question