Solved

Problem with mapping a stored procedure to a TableAdapter class

Posted on 2006-07-21
6
294 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Closed, 500 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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