Logged in user displays user specific data from database

Hi there - I am fairly new to ASP.NET and am hoping for some guidance here ...

I am building an application for manages to track employees as the employee leaves the business.  Basically there are processes a line manager is required to complete and I want the manager to enter completion dates when those processes have been completed.

I have gotten as far as successfully creating the login/membership controls.  The user (line manager) logs in and is then redirected to the correct page and thats fine.  However, I then want for the user to see records from a database for the employees reporting to them (this will be maintained by an admin person elsewhere.)  The database record for the employee who is leaving holds the username of the line manager.

I was thinking that I should be able to link the username of the line manager logging in to the web application to the database so that only the employee records assigned to the line manager are displayed.

I tried to create a gridview control linked to the database.  The gridview control on its own works fine in so much as it returns the correct data from the database - but it obviously returns all the records in the database.  I then tried to add a WHERE clause, so something like:

SELECT [id], [linemanager_user], [leaver] FROM [tblLeavers] WHERE ([linemanager_user] = 'this should be the logged in username')

However, I am unable to select the username from the login username control as it is not available as an option in the Configure Data Source Add WHERE clause dialogue box.

Can anyone help out?

 Thanks in advance.

0
cjmcc5150
4/3/2009 11:02:32 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

1 Replies
853 Views

Similar Articles

[PageSpeed] 19

If you are manually Binding your GridView using the ADO.NET way then you can do something like this:

VB.NET

Here's an example below:
 

Public Function GetData(ByVal user As String) As DataTable
    Dim dt As New DataTable()
    Dim connection As New SqlConnection("YOUR CONNECTION STRING HERE")
    Try
        connection.Open()
        Dim sqlCmd As New SqlCommand("SELECT * FROM TableName", connection)
        Dim sqlDa As New SqlDataAdapter(sqlCmd)
        
        sqlCmd.Parameters.AddWithValue("@user", user)
        sqlDa.Fill(dt)
    Catch ex As System.Data.SqlClient.SqlException
        Dim msg As String = "Fetch Error:"
        msg += ex.Message
        
        Throw New Exception(msg)
    Finally
        connection.Close()
    End Try
    
    Return dt
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        Dim dt As DataTable = GetData(Page.User.Identity.Name)
        If dt.Rows.Count > 0 Then
            GridView1.DataSource = dt
            GridView1.DataBind()
        End If
    End If
End Sub
 

 C#

 

public DataTable GetData(string user)
{
    DataTable dt = new DataTable();
    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
    try {
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT * FROM TableName", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        
        sqlCmd.Parameters.AddWithValue("@user", user);
        sqlDa.Fill(dt);
    }
    catch (System.Data.SqlClient.SqlException ex) {
        string msg = "Fetch Error:";
        msg += ex.Message;
        
        throw new Exception(msg);
    }
    finally {
        connection.Close();
    }
    
    return dt;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack) {
        DataTable dt = GetData(Page.User.Identity.Name);
        if (dt.Rows.Count > 0) {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}
  
Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
4/3/2009 12:29:02 PM
Reply:

Similar Artilces:

How do I display data in a datagrid specific to the logged in user using the User ID
Hi, I've got a table that holds user details (such as age, location etc). This table is linked to a user logon details table by the UserID column. How do I get a datagrid to display the logged on users details?  Everything I've read so far uses Username but username isn't a column in my UserDetails table. I've tried SELECT all from the user details table and using UserID as a session parameter but the datagrid doesn't display so I'm doing something very wrong I think. The sql statement looks like this:  SELECT UserID, FirstName, Surname, Telephone, Sect...

Cannot open user default database. Login failed. Login failed for user 'DOMAIN\User'. (.Net SqlClient Data Provider)
Hi when i try and open a database in sql server management studio i get the error "Cannot open user default database. Login failed. Login failed for user 'DOMAIN\User'. (.Net SqlClient Data Provider)", what can i do to rectify this, i have googled around and still havent found no answers. Which authentication mode is your Sql Server configured - MixedMode, Windows Authentication or Sql Server Authentication? Please refer to http://msdn2.microsoft.com/en-us/library/ms366351.aspx and http://blogs.msdn.com/sql_protocols/archive/2006/02/21/536201.aspx    Ple...

display data for a particular logged in user (User.Identity.Name;)
hi, basically i have a database table with: fileNo, fileName, fileSize, fileType, fileOwner etc at the minute my grid view displays ALL files for every fileOwner, how does one filter this to only displaying the files which belong to a particular fileOwner who is logged in.. i.e. SELECT * FROM tblFile       WHERE fileOwner = (User.Identity.Name)         -   to show files for a user who is logged in   how does one do this please?   thanks and regards Hi,jules U can Do like thisI am assuming that ur ...

Login Control logs user in, but User is not authenticated, but user really is!
I am using aspnetmembershipprovider.  Everything works fine.  I slap a login control on my login page and am able to login and get to my secure folder etc.  When I try and put some code in the LoggedIn method "User.Identity.IsAuthenticated" = false, yet if I blow through that, I am logged, authenicated etc.  The documentation says that the LoggedIn event is "after the user is authenticated".  Any Ideas what is up?Thanks,TPS------------------------------------------------------Note Collaboration for your next confernce call.http://www.ConferenceCallNotes.com I am...

display data from database accordingly when users log in
  the situation is after someone loggs in with his credentials then on his page he should be displayed data relating only to his pageand so and if someone else loggs in on his page his data from the database such as sql server 2005 should be displayedplease helo out if anyone know how to do this in asp,net c# Hi,aditya Try This Example In sql CReate table register (id int identity ,[UserName] varchar(20),[Password] varchar(20), varchar(30)) GO insert into register Select 'User1','Pass1','Email1' union all Select 'User2','Pass2',...

Display data in a datagrid that is specific to only the logged in user
How do you display data from tables that is based on the logged in username.  I know that some post are saying to use the "GetUser" method via Membership.  Like for example, I want to display all vendor information for a specific vendor that has logged onto the application in a datagrid?  The username in the database is the same as the VendorID.  I'm using Visual Studio 2005 and a SQL Server 2005 database.   Any feedback would be greatly appreciated. Thanks, rsandes2 (Still a newbie) Hi, If your users are logging into website and the login name m...

After a user has entered his registration info I want to add him to my database. Is there a specific control which can handle the update/insert of data from a form into a database?
After a user has entered his registration info I want to add him to my database and also validate whether his name/e-mail is already registered. Is there a specific control which can handle the update/insert of data from a form into a database?If not, how can I add someone? Basically what im asking is how can I add a new user using the best method (best functionality) of the .NET framework. So validate duplicates AND add this user. ;)please mark answers as 'Answered' and post back solutions when you figure stuff out that isnt in the post already. Hi, you could use this sample as a...

User Control Passing Data To Another User Control?
I got two user controls in a .aspx page. one user control contains a list of names along with the user id as that listbox .value property. now when the user selects a user a clicks on a command button it needs to send the value of the selected user name to another user control in the same form name shippingInfo; Anyone know how to do this; I keep getting a "Object reference not set to an instance of an object" error message here is the code for the MyContacts control (the one with the user name and user id value) Imports PackageDeliveryVB.ShippingRequestInfo Public Class...

UpdatePanel within a web user control resets data of controls outside the user control
Hello, I have a usercontrol on my page which displays a running marquee text. It makes use of an UpdatePanel (within the user control) to refresh the marquee text (within the updatepanel) at periodic intervals. For this I added a timer control and its tick event as an async postback trigger for the UpdatePanel. I have a lot of textboxes and dropdown list boxes (all with runat=server) on the page outside the above user control. These are also contained in different UpdatePanels. Problem is, when the user control does a postback to refresh the marquee text, it causes some of...

User Control in a User Control
I have a first user control that contain a second user control. If I cahe the first user control can I still dynamically control the second one or it is cached with the first one therefore can not control it anymore? Thanks When your first control is cached, its rendered out and its output is stored in the cache. On every request to the page containing your control that can satisfy it from cache, the output will just simply be inserted - your first control will never be instantiated or run in that case, so the second one wont be either. Hope this helps,Mike VolodarskyProgram Manager...

User Controls in User Controls?
I have the follow set up and it is throwing an error "Unhandled error loading module" ... is there some trick to this that I don't know?I have a project A that uses controls from project B (project B is just a project with all my user controls used through out other modules).  project A uses user control 1 from project B with out any issues at all - great.  But then I wanted to add user control 2 to user control 1 and that is when I get my error.  In fact I don't even have the user control 2 in user control 1, just when I put the register part into user control 1 is when it al...

User control in a user control
Hello all, I have created a user control with some checkboxes, and public properties to get the values of these checkboxes. I have also created a second user control using the first user control; this second control also access the public properties of the first one. My problem is when I implement the second control in an aspx page, if I click at runtime the checkboxes in the first control, I am able to access its public properties from the methods of the second control but the values of these properties are wrong. while at design time, I do not have this problem. Than...

user control to user control
I have an aspx page that have a "Register" directive to two usercontrol called rptMaster.ascx and rptDetail.ascx. I'm trying to set up a Master-Detail relationship and wish for the rptMaster.ascx to trigger and event from a dropdownlist and for the rptDetail to be listening for DropDownList event. I believe I have gotten the events part okay with help from this articel http://codeproject.com/aspnet/Page_UserControl.asp but my declaration for a private statement to my rptMaster class is producing an error. I'm using beta 2 of asp.net and I 'm having trouble getting a reference TO ...

Retrieving Data from a Child User Control While in a Parent User Control
I have a User Control that has several Nested (Child) User Controls in it. When I do the final button click (in the Parent User Control) I need to be able to access the Child Controls info (TextBox, DropDownList’s, etc) from the Parent Control. I have tried using the FindControl(“”) function and many others with no success (generally I get a null reference exception when trying to read the data). I have tried referencing the Child Control then using that reference to access the Child element (TextBox, etc) with no success. Has anyone else done this? If so, could you share some...

Web resources about - Logged in user displays user specific data from database - asp.net.presentation-controls

Samples of display typefaces - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Apple applies for patent to extend the Apple Watch display into woven fabric bands
The U.S. Patent and Trademark Office has today published an Apple patent application for incorporating displays into woven fabric. While the ...

Rumor: Apple again said to use OLED display for 'iPhone 8' in 2018
Yet another report from Japan claims that Apple is planning to jump to OLED panel displays for its iPhone lineup starting in 2018, using the ...

Twitter to display your selfies and cat photos better
... photos, so you can experience and present them as they were meant to be viewed. We're also introducing larger, more beautiful multi-photo displays, ...

Report offers more evidence Apple is moving to OLED iPhone displays
... until 2018 at the earliest, but it looks more and more certain that at least some of Apple's future iPhones will be equipped with AMOLED displays ...

Google Cast Remote Display Plugin for Unity
Posted by Leon Nicholls , Developer Programs Engineer Today we launched the Google Cast Remote Display plugin for Unity to make it easy to take ...

PPC New Year’s Resolutions: Power-up Your 2016 Paid Search, Display & Social Advertising Campaigns
PPC New Year’s Resolutions: Power-up Your 2016 Paid Search, Display & Social Advertising Campaigns was originally published on BruceClay.com, ...

Twitter tweaks photos, displays them in full on your timeline
The social media platform has tweaked the way it displays pictures, showing them in full in timelines and altering multi-image posts.

None more black: LG’s OLED displays show that nothing is really something
This post is brought to you by LG Electronics. When it comes to TVs, more is the new black. As the visual fidelity of film and television gets ...

Media Hypocrisy On FULL Display In Wake Of San Bernardino Shooting
Media Hypocrisy On FULL Display In Wake Of San Bernardino Shooting

Resources last updated: 12/9/2015 12:59:14 PM