Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FormView - Multiple DataKeyNames - how do I retrieve one of the values?

Posted on 2010-11-25
10
Medium Priority
?
1,573 Views
Last Modified: 2012-05-10
Hi
I need to get the value of one of CostID from my DataKeyNames in a FormView.  I have tried:

DataKey key = fvCostJob.DataKey["CostID"];
string costID = key.Value.ToString();

My code is:
<asp:FormView ID="fvCostJob" runat="server" CellPadding="4"
            DataKeyNames="CostID,LoadID" DataSourceID="SqlDataSource1" ForeColor="#333333">

The line that's causing the problem is:
DataKey key = fvCostJob.DataKey["CostID"];
Intellisense tells me "cannot implicitly convert type 'Object' to ...DataKey"
0
Comment
Question by:Lorna70
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34212068
The DataKey collection doesn't return a DataKey object it returns the actual value of the key. So, if your CostID is an integer you would do something like:

    int costId = int.Parse(fvCostJob.DataKey["CostID"].ToString());

Open in new window

0
 
LVL 3

Accepted Solution

by:
Sokrates earned 1000 total points
ID: 34212280
DataKey key = fvCostJob.DataKey["CostID"]; ----> IS NOT NESESSARY

TRY THIS:
string costID = fvCostJob.DataKey["CostID"].ToString();
0
 
LVL 5

Expert Comment

by:Greg Gamble
ID: 34213018
string xxx = fvCostJob.DataKey["CostID"];
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34290396
Nice of you to ignore my post! Given that I put exactly the same thing earlier (ok, i assumed CostID was an int so did the cast, but the principle was the same)
0
 
LVL 3

Expert Comment

by:Sokrates
ID: 34290503
With all the respect...
carl_tawn you are right to a point. If you see question it says:

DataKey key = fvCostJob.DataKey["CostID"];
string costID = key.Value.ToString();

Read carefully and answer straight to the point. If you compare our answers you will see:
1) you did not mention that  DataKey key =.... is not needed
2) costID is a string not an integer.
my answer was staight to the point

peace
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34290533
Actually I think you'll find that I explained that the DataKey collection doesn't actually return a DataKey object, which is a little more detailed that "IS NOT NECESSARY".
0
 

Author Comment

by:Lorna70
ID: 34290576
Sorry Carl but I was in a hurry so just gave points to the best answer.  I think you've helped me before so next time I'll make sure I allocate points to you :-)  Thanks.
0
 
LVL 3

Expert Comment

by:Sokrates
ID: 34290746
The question was "I need to get the value of one of CostID from my DataKeyNames in a FormView" and two lines of code attached:
DataKey key = fvCostJob.DataKey["CostID"];
string costID = key.Value.ToString();
clean and clear i gave the solution..

"The DataKey collection doesn't return a DataKey object it returns the actual value of the key" it is not clear that is not needed.
0
 

Author Comment

by:Lorna70
ID: 34290785
Now now boys lol!  You have both been really helpful and as I said, I'll assign points to Carl the next time he helps.  Think no more should be said now :-)
0
 
LVL 3

Expert Comment

by:Sokrates
ID: 34290834
No worries Lorna70. Thanks for the points.     :)
carl_tawn i apologise if i disrespect you in any way...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

722 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