?
Solved

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

Posted on 2009-07-15
3
Medium Priority
?
241 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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 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