Solved

Problem with mapping a stored procedure to a TableAdapter class

Posted on 2006-07-21
6
295 Views
Last Modified: 2008-02-01
(Using .NET 2.0)
I have a stored procedure as follows:
ALTER PROCEDURE dbo.GetUnallocatedHours
      (
      @employeeName varchar(200)
      )
AS
      declare @numHours real
      SET NOCOUNT ON
      SET @numHours = (SELECT SUM(HoursTotal) FROM ClockCard WHERE Employee = @employeeName)
      RETURN @numHours

Then, using the DataSet designer I mapped thie stored procedure to the ClockCard DataTable, giving it the name GetUnallocatedHours, and in the "shape of the data returned" part of the wizard, I chose to return "A single value" (the second option). In the designer, if I right-click this new query and choose 'Preview Data' it works as expected. But when I try to call it from the code below, I get the famous error "object reference not set to an instance of an object":

MessageBox.Show(clockCardTableAdapter.GetUnallocatedHours(employeeName).ToString());
this doesn't work either: MessageBox.Show(Convert.ToInt32(clockCardTableAdapter.GetUnallocatedHours(employeeName)).ToString());

When I hover over the 'GetUnallocatedHours' in the code above, IntelliSense displays: int? ClockCardTableAdapter.GetUnallocatedHours(string employeeName) which seems right although I don't understand the question mark.

The function works correctly because it works in the designer. But how do I correct my code to call it correctly from there?
0
Comment
Question by:smickell
  • 3
6 Comments
 
LVL 6

Expert Comment

by:NunoGodinho
ID: 17158623
Hi,

 Before you user this:
     MessageBox.Show(clockCardTableAdapter.GetUnallocatedHours(employeeName).ToString());
  Are you initializing the clockCardTableAdapter = new ClockCardTableAdapter();?

  It could be it.
0
 

Author Comment

by:smickell
ID: 17159638
I am indeed. A few lines before it in the same method I use:
DataSet1TableAdapters.ClockCardTableAdapter clockCardTableAdapter = new DataSet1TableAdapters.ClockCardTableAdapter();

I tried removing this and dragging a TableAdapter class onto the form in the designer but still no go.
employeeName is also set correctly as I have tested it.

*Note - I only get the above error message if I use clockCardTableAdapter.GetUnallocatedHours(employeeName).GetValue().
If I try clockCardTableAdapter.GetUnallocatedHours(employeeName).Value the exception says 'Nullable object must have a value.'
.HasValue returns false so in other words the method is returning null, not an object reference error.
Convert.ToSingle and Convert.ToInt32() both return 0 whereas if testing the procedure in the DataSet designer, it should be the correct value of 9.
0
 

Author Comment

by:smickell
ID: 17161050
Never mind, I spent an hour or three fiddling around with the settings. It was down to a combination of the code in the stored procedure and the way it was  mapped to the tableadapter. The parameter settings were mainly at fault.
0
 

Author Comment

by:smickell
ID: 17162557
The code for the stored procedure was:
ALTER PROCEDURE dbo.GetUnallocatedHours
     (
     @employeeName varchar(200)
     )
AS
   declare @numHours real
   SET @numHours = (SELECT SUM(HoursTotal) FROM ClockCard WHERE Employee = @employeeName)
   RETURN @numHours

For the stored procedure to work correctly when mapped to a TableAdapter I had to have the return statement as a SELECT, I couldn't return a parameter. This is weird because as I said, it works when previewing the mapped method from the designer. Anyway, the code that worked is simply:

ALTER PROCEDURE dbo.GetUnallocatedHours
     (
     @employeeName varchar(200)
     )
AS
SELECT SUM(HoursTotal) FROM ClockCard WHERE Employee = @employeeName

Easy as that.
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17185345
Closed, 500 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now