 |
BOOK: Beginning ASP.NET 4 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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
|
|
|
|
|

July 13th, 2011, 05:51 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Cliff,
Quote:
|
I only want to show/work with "members" that were added by login. When simulating this locally I used Profile.Username.
|
Not sure what you mean with this....
Quote:
|
Great book. Thanks for all the help as I appreciate your time.
|
Thank you. And you're welcome.
Quote:
|
I'm sure there will be a few more postings, but I am trying to do this myself as that's how one learns
|
Absolutely!
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 13th, 2011, 07:41 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
Using Where filter
In one of my pages I have a Page_Load method that does the following:
var query = from Customer in db.Customers
where Customer.UserName == Profile.UserName
select Customer.Customer_ID;
//Now we have found the Customer_ID value based upon the Profile.UserName
//Convert that value to string
//Create a TextBox (CustomerID) based upon TextBox1 (Customer_ID) to show the Customer_ID value in the DetailsView
foreach (var result in query)
{
value = result.ToString();
TextBox CustomerID = ((TextBox)DetailsView1.FindControl("TextBox1"));
CustomerID.Text = value;
}//end foreach
Essentially what is happening here is that I am "filling in" a TextBox for my customers with the Customer_ID value (int) to make things a bit easier in the DetailsView section (less data input) and maybe reuse that value below.
Ok, but now in the GridView I only want to show only those players that were entered by the customer identified by a specific Customer_ID.
In my SqlDataSource definition for GridView I have:
SelectCommand="SELECT [Customer_ID], [Player_ID], [Player_Name], [Handicap] FROM [Player]"
...since I will have lots of "players" in the Player table that have been added by multiple customers, but I only want to show (GridView) those players that have been added by the currently logged in customer.
I need some type of where filter to finish the Select statement? No?
Could it be ".....Where [Customer_ID] = Customer.Customer_ID
or would you recommend using a LINQ and a LinqDataSource as the DataSourceID for the GridView.
Hope this makes some sense
Kindest thanks
Cliff
|
|

July 14th, 2011, 07:32 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Rather than using Profile, you can access the name of the looged in user like this:
Context.User.Identity.Name
Maybe these posts help:
http://p2p.wrox.com/search.php?searchid=477317
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 14th, 2011, 10:25 AM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
Post link led to no matches?
Hi
Sorry, but the link you gave led to no matches?
When someone logs in I find that username in a table that also has an auto updated CustomerID (just those two fields in that table). What I am after is the integer CustomerID to be used in the WHERE clause of the SelectCommnad statement so as to show only those members (GridView control) based upon the CustomerID (see code from Page_Load)
I have (or can get with Context.User.Identity.Name) this integer value, but how should I incorporate that value found via Page_Load into a Select command.
Cliff
|
|

July 14th, 2011, 10:39 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Cliff,
Hmmm, looks like searches are only valid for some time. Try this:
1. Go here: http://p2p.wrox.com/search.php
2. Search for "Context.User.Identity.Name" (with the quotes)
3. In the user name field enter Imar
This yields a few posts that deal with this topic. Basically, you handle an event such as Selecting and programmatically assign the value.
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 14th, 2011, 02:46 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
Limiting GridView results
In the first part of the code I obtain the Customer_ID (an integer) via a query. I then use that value to populate a textbox, but much more importantly I would like to limit the GridView results on the page by taking that value and assigning it to @theCustomer in my Selectcommand statement
var query = from Customer in db.Customers
where Customer.UserName == Profile.UserName
select Customer.Customer_ID;
//Now we have found the Customer_ID value based upon the Profile.UserName
//Convert that value to string
//Create a TextBox (CustomerID) based upon TextBox1 (Customer_ID) to show the Customer_ID value in the DetailsView
// and limit the results (GridView) to the logged in Customer_ID value
foreach (var result in query)
{
value = result.ToString();
theCustomer = Convert.ToInt32(value);
TextBox CustomerID = ((TextBox)DetailsView1.FindControl("TextBox1"));
CustomerID.Text = value;
e.Command.Parameters["@theCustomer"].Value = theCustomer;
}//end foreach
In the SqlDataSource SelectCommand I have a reference to @theCustomer in my WHERE filter and then call the method SqlDataSource1_Seclecting which is where the above code resides.
SelectCommand="SELECT [Customer_ID], [Player_ID], [Player_Name], [Handicap] FROM [Player] WHERE ([Customer_ID] = @theCustomer)" onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter Name="Customer_ID" Type="Int32" />
</SelectParameters>
My error message is the following:
An SqlParameter with ParameterName '@theCustomer' is not contained by this SqlParameterCollection.
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.IndexOutOfRangeException: An SqlParameter with ParameterName '@theCustomer' is not contained by this SqlParameterCollection
Just wondering if you see anything obvious, my code seems to follow examples that I have googled?
Thanks again as this is the key to my "show"
Cliff
|
|

July 14th, 2011, 04:25 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Did you look at the posts I (tried to) link to? For example, this one:
Displaying data based on user logged in
shows you how to use the Selecting event of the SqlDataSource to do exactly what you want. Also, make sure your names libe up. Your parameter is called Customer_ID and thus you need to use Customer_ID elsewhere as well. The example I linked to uses UserName but the idea is the same.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

July 15th, 2011, 11:20 AM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
|
|
Thanks!
Just wanted to let you know that re-reading the Post you suggested fixed my problem.
I certainly do appreciate all the information as I would have "wandered about" for probably sometime.
Didn't mean to be a pest and I really am trying to do this myself, but being in the "apprentice" stage I often need to look to a mentor for help.
Your response to my posts were better than any forum I have ever been a part of and once again the book was terrific.
Thanks,
Cliff 
|
|

July 15th, 2011, 11:56 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Didn't mean to be a pest and I really am trying to do this myself, but being in the "apprentice" stage I often need to look to a mentor for help
|
Don't worry about that; you're definitely not a pest. You picked a Beginner's book, so the question you're asking here all make perfect sense.
Quote:
|
Your response to my posts were better than any forum I have ever been a part of and once again the book was terrific.
|
Thank you. Spread the word, spread the word.... ;-)
Cheers,
Imar
|
|
 |
|