Solved

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

Posted on 2009-07-15
3
233 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
Comment Utility
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
Comment Utility
I will try it and let you know what happens.
0
 

Author Comment

by:cesemj
Comment Utility
Excellent insight.  Thank you again...
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now