Solved

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

Posted on 2009-07-15
3
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

740 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