Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with mapping a stored procedure to a TableAdapter class

Posted on 2006-07-21
6
Medium Priority
?
302 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

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 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