Wrox Programmer Forums
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals 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
 
Old July 2nd, 2008, 09:54 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

I'm not the only one that gets confused with the relationship of DataKeyField/Names and the values:

http://forums.asp.net/p/1269097/2393266.aspx#2393266

What is confusing is on my Gridview (code above) I do have a memberID bound column - so why would I need to put "memberID" in the DataKeyNames field?

<asp:BoundField DataField="memberID" HeaderText="memberID"
                InsertVisible="False" ReadOnly="True" SortExpression="memberID" />

This wouldn't be so bad if people didn't explain this so many different ways. It's said that DataKeyNames is used for primary key - but then threads say you can use it for any field that you don't want bound - but then my control has the field bound and won't work without the DataKeyName....




 
Old July 3rd, 2008, 04:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

To reiterate my previous post: you are using a control that doesn't support this.

In .NET 1.x there was the DataGrid and the DataList, both supporting single keys. In .NET the GridView was introduced to replace the DataGrid. The GridView offers a much better technical design than the DataGrid.

Now with .NET 3.5 you have the ListView. Just as the GridView is a better alternative to the DataGrid, the ListView is better than the DataList. For example, it supports multiple keys using the DataKeyNames property and offers superior support for editing and displaying of data.

So, do yourself a favor: ditch the DataList and use a ListView instead. You'll find that the way it works is very similar to the DataList. I don't think you can do what you want to do with the DataList, unless you use other techniques described earlier.
Quote:
quote:This wouldn't be so bad if people didn't explain this so many different ways.
Ask someone for the road to Rome; you'll get as many different answers :-) That's part of the programming deal you need to get used to. There are many different controls, different properties, different ways to handle them, different versions of the .NET Framework, different views on the matter, different programming languages, different preferences, and many, many different ways to accomplish the same thing.

The cure is: Google till your fingers are sore, use the debugger, read, read, read and read some more, debug some more, and then choose something that works for you. Don't keep saying things like "I don't understand it because it's not in my book" There are a zillion things that are not in your book. However, they are available on Google, and right there, in your face, in the Debugger and Watch window. Learn to use these tools, learn how to use the MSDN documentation site, learn how to use Google, and you'll be a much better programmer .... ;)

Cheers,

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.
 
Old July 3rd, 2008, 10:20 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Imar,

I appreciate your comments. Trust me, I have spent many hours on researching this - both Google, Metacrawler, MSDN - and actually finally realized that the DataList doesn't support multiple keys.

What I am failing to explain is that the question had a twist in really understanding "how" datakeys work-has nothing to do with the DataList vs GridView vs List--much broader. *All* of the research I have done simply states "set the primary key as your DataKeyName/Field" - but doesn't explain what is going on behind the scenes.

The reason this confuses me is that in the simple example you had me do (with SQLDataSource and Gridview) - I made *no* changes. It displays all the fields from my table and has "bound fields" for them - so, in the control, you have a boundfield for my primary key, "memberID". At the same time, you have bound fields for all the remaining fields.

NOW - I also have the <asp:parameters> in the Delete, Update and Select Commands that has *all* the parameters - thus I have a "memberID" parameter.

So - how does DataKey work is really my question - and I can't seem to find an article that explains it. What I have read is that you can use DataKey if you don't want to bind to a control within your List or GridView, etc. So, having said that, why does the GridView fail when I remove the DataKeyName property--EVEN THOUGH I have a boundfield for memberID - and a Parameter for memberID.

Regardless of what control I use - I am having difficulty understanding simply - how DataKeys work--what they are doing. All the stuff I have read simply states to set the datakey as the primary key - without explaining the "why" you are doing that.

I am trying to understand this to see if I could simply put all my table fields in the datakey without actually binding them to a control - and then refer to them in codebehind and set them all up myself - and what impact that would have.

Sincerely,
Rob

 
Old July 3rd, 2008, 11:14 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Ok...I **THINK** I might have my answer - and I also think my confusion was stemming from various things happening at one time tha weren't related.

If someone could please confirm / deny my statements, I'll be able to move on.

a) If you simply use a SqlDataSource and a control (ListView, GridView, etc) then DataKeyName/Field has *NO* impact. This is because the SqlDataSource has "update" commands and parameters that does all the transferring of fields to parameters.
b) The DataKeyName/Field property really comes into play when you are wanting to tie to it in codebehind - for example, if you want to tie into a "Select" command and do something with the primary key - at which time you would reference the correct DataKey parameter and do with it as you will.
c) DataKeyName is sorta like the "SelectedValue" of a dropdown. You can store as many fields as the control will allow - and then simply refer to them as you will in the code behind.

Sincerely,
Rob

 
Old July 4th, 2008, 02:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

1) Yes

2) Yes

3) No, not really. DataKeyName only contains the name of the key. .NET uses this to understand what columns to keep track of. The actual key values are stored in the DataKeys property.

If you really want to understand the *why*, download a tool called Reflector. It allows you to look into the ASP.NET assemblies, so you can dismantle the DataList control and look at its internal behavior.

Alternatively, you could choose to debug into the .NET Framework: http://weblogs.asp.net/scottgu/archi...libraries.aspx

Cheers,

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.
 
Old July 4th, 2008, 11:41 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

*THANK YOU* makes much more sense now!!

 
Old February 19th, 2009, 10:05 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help please...

Okay, I’m stumped. I have membership enabled and I am also storing additional user information in a separate table (not in the aspnet_provider table).

I am trying to create a summary view of the user’s information that they submitted at time of registration.

I am having difficulty referencing the membership Guid of the current user to their additional information stored in the other table (the Guid and P_USERID need to match).

Here’s my code:

Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
 
  public partial class myCVSummary : System.Web.UI.Page
  {
      DataTable dtResult;
      MembershipUser currentUser;
      Guid myID = new Guid();
 
      protected void Page_Load(object sender, EventArgs e)             
      { 
          currentUser = Membership.GetUser(myID); 
 
          if (!IsPostBack)
          {   
              //I’m having difficulty with statement:             
              dtResult = GetmyInfo(this.Request["id"].ToString());
 
              if (dtResult.Rows.Count > 0)
 
              {
 
                  //Basic Information
                  mTitle.InnerText = dtResult.Rows[0]["SALUTATION"].ToString();
                  mFirstName.InnerText = dtResult.Rows[0]["FIRST_NAME"].ToString();
                  mLastName.InnerText = dtResult.Rows[0]["LAST_NAME"].ToString();
                  mSuffix.InnerText = dtResult.Rows[0]["DESIGNATION"].ToString();
                  mGender.InnerText = dtResult.Rows[0]["GENDER"].ToString();
                  mCitizenship.InnerText = dtResult.Rows[0]["CITIZENSHIP"].ToString();
 
 
              }                            
          }
      }
 
      private DataTable GetmyInfo(string myInfoId)
      {
          SqlConnection sqlConn;
          SqlCommand sqlCmd;
          SqlDataAdapter sqlDA;
          DataTable dtResult = new DataTable("Result");
          string cmdText = String.Empty;
          cmdText = " SELECT *  FROM myDatabaseTable WHERE (P_USERID = '" + myInfoId + "')";
          try
          {
              ConnectionStringSettings connString = WebConfigurationManager.ConnectionStrings["myConnectionString"];
              if (null != connString)
              {
                  using (sqlConn = new SqlConnection(connString.ConnectionString))
                  {
                      sqlCmd = new SqlCommand(cmdText, sqlConn);
                      sqlDA = new SqlDataAdapter(sqlCmd);
                      sqlDA.Fill(dtResult);
                  }
              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
          return dtResult;
      }
  }
 
…any help would be much appreciated.

~P
 
Old February 21st, 2009, 06:35 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take a look at this:

Guid myID = new Guid();

....

currentUser = Membership.GetUser(myID);

What do you think this code does? It generates a completely random new GUID and then tries to retrieve a user based on that guid. Given the nature of GUIDs, you won't have a user with that ID in your database so currentUser will always be null.

Instead, cast the ProviderUserKey on the current user to a GUID if you need its ID.

The first line of the next example retrieves the currentlly logged on user (or null if the user is not logged in) and the second line shows how to retrieve the user's GUID:

MembershipUser myUser = Membership.GetUser(); // () gets the current user.
Guid userId = (Guid) Membership.GetUser().ProviderUserKey;

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 21st, 2009, 12:37 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That did it! You-da-man!

Special thanks for pointing out the error of my logic! With your explanation and example (and struggling for a few minutes... which forced me to think), I was able to get the result I was after.

Thanks for taking the time and being so prompt in your response.

~Patrick
 
Old February 21st, 2009, 12:45 PM
Registered User
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question

how di create a file with windows vistahttp://p2p.wrox.com/images/icons/icon5.gif





Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Membership provider question aspcoder BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 July 27th, 2008 09:59 AM
Dim user As MembershipUser = Membership.GetUser(us sstuber BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 May 8th, 2008 03:42 PM
Membership User kulkarnimonica ASP.NET 2.0 Professional 2 July 12th, 2007 04:55 PM
problem with creating membership user hertendreef ASP.NET 2.0 Basics 1 April 9th, 2007 02:37 PM
Migrate user data to new membership model relaytest49 ASP.NET 2.0 Professional 2 May 24th, 2006 09:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.