Solved

Problem with mapping a stored procedure to a TableAdapter class

Posted on 2006-07-21
6
298 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
[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
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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