 |
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

November 19th, 2008, 02:45 PM
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to use login user name in where clause?
Hi there, I am new to ASP.net. I have VWD Express 2008 and SQL Server 2008 Express installed to create a simple web application for learning purposes. I have used Log in page and I carry the user name to a page where I have my gridview control. In data source configuration I want to able to use the username in where clause. Can someone please help me with this, and show me simply as possible, what I need to do? What and where exactly I need to place the code. Thanks for your help in advance. gsrai31
|

November 19th, 2008, 04:26 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Are you using a SqlDataSource control? If so, you can do something like this:
1. Configure the data source and create a parameter in the wizard. You can set its type to None as you'll fill in the value later. Your SQL will end up like this:
SELECT WhatEver FROM SOmeTable WHERE UserName = @userName
Inside the SqlDataSource you see the <Parameters> with your parameter.
2. Handle the Selecting event of the SqlDataSource. In code behind, inside the handler, write something like:
e.Command.Parameters(0}.Value = YourUserName
If you're using ASP.NET Membership you can get the name from the MembershipUser which you can get by calling Membership.GetUser() (provided the user is logged on).
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

November 19th, 2008, 05:20 PM
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar
Many thanks for looking into my request. I am still slighly lost here. Just to clarify couple of things. I am using SqlDataSource control. I am also using ASP.NET Membership so I have ASPNETDB.MDF for user management. Another SQL Server database I am using for my data management purposes is called SPMSDB.MDF. On my log in page I have used the login control, leaving the names etc as default. Login process works fine. Once users has successfully logged in, he is sent to a page called refdata.aspx. This page has a LoginName control and a datagrid. My select statement is meant to look like this -
Select * From [RefData] Where ([SupplierName]) = "UserName"
Following your instruction above, when in Add WHERE Clause window I select Column "SupplierName" Operator = Source: None, the Where Clause added to the statment is [SupplierName]=@SupplierName
So what code I need to add in the code behind, inside the handler? Is there anything else I need to do to make my gridview work?
Sorry about this, hope this all makes sense. Thanks again for your help.
gsrai31
|

November 19th, 2008, 05:31 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You're pretty much there, if you add the code from my previous post.
Your SqlDataSource should look similar to this:
Code:
<asp:SqlDataSource ...
SelectCommand="Select * From [RefData] Where ([SupplierName]) = @SupplierName)">
<SelectParameters>
<asp:Parameter Name="SupplierName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Then in code behind you can have something like:
Code:
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) _
Handles SqlDataSource1.Selecting
e.Command.Parameters(0).Value = Membership.GetUser().UserName
End Sub
Right before the SqlDataSource talks to the database, it fires the Selecting event. Inside the handler, you grab the user name from the MembershipUser and feed it to the parameters collection (since you only have one, you set it on (0) which is the first parameter).
The SQL statement is then executed with the user's user name in the WHERE clause.
Note that this only works when the user is logged in when accessing this page. Otherwise, GetUser() returns Nothing....
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

November 20th, 2008, 06:46 PM
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar
Thanks again. Can you please tell me how do I check if my SqlDataSource looks like this -
asp:SqlDataSource ...
SelectCommand="Select * From [RefData] Where ([SupplierName]) = @SupplierName)">
<SelectParameters>
<asp:Parameter Name="SupplierName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
This is what my select statement looks like in Configure Data Source - SqlDataSource1 window -
SELECT * FROM [RefData] WHERE ([SupplierName] = @SupplierName)
I have used the the following code in the code behind as you suggested -
e.Command.Parameters(0).Value = Membership.GetUser().UserName
When I log in and go to the page refdata.aspx I get an error as shown at the bottom of this message. By the way I have disabled the strong password, so does this mean I am not using ASP.NET Membership? Or is there another reason for this error?
Thanks again for your help with this. gsrai31
Server Error in '/SPMS' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
|

November 20th, 2008, 07:07 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You can check the code for the SqlDataSource control in markup, where all the other HTML and markup is.
I think the error is caused by the fact the user is not currently logged in. To check that out, set a breakpoint on the line that starts with e.Command.... (hit F9 to do so) then hit F5 to start debugging. Once you hit the line with the breakpoint, look at Membership.GetUser() (you can select that text, right-click it and choose Add Watch. ) If the item is Nothing / Null, you're not logged in.
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

November 21st, 2008, 07:38 PM
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
I built all the pages before viewing in browser and testing the website. It worked fine, did not get the error message. So the error must be due to the fact that user was not logged in as you pointed out.
I am sure I will run into more problems while working on this application. I will be looking for furhter help over the next couple of week. Your help will be much appreciated.
Thank you again. gsrai31
|
|
 |