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

x
?
Solved

How do I use a .NET tableadapter to retreive a scalar value from a Stored Procedure

Posted on 2009-03-30
9
Medium Priority
?
1,141 Views
Last Modified: 2012-06-21
I have been having issues retrieving scalar values from scalar tableadapter queries.

The tableadapter was set up to use an existing, TESTED, storedprocedure.

I can PREVIEW the expected data...

But in retrieving the value in code I keep getting cast type errors.

I have check to make sure the return type of the qure


MyDoubleValue = Convert.ToDouble(this.MyTableAdapter.GetInterestReceived("1O2", "9050",  "9205", PeriodStart, PeriodEnd));

Open in new window

0
Comment
Question by:hertzgordman
[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
  • 5
  • 4
9 Comments
 

Author Comment

by:hertzgordman
ID: 24024250
BTW I have "coded" the function using the SP and the executescalar() technique.

It works but seems like more effort than necessary...
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 24025195
Need more info, what is inside GetInterestReceived method?
0
 

Author Comment

by:hertzgordman
ID: 24025237
Its a select statement retrieving the sum of a filtered set of transactions.

Would be comparable to adding up the sales for the customer for a date range.

Returns ONE field which is the total field.
0
Industry Leaders: 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 18

Expert Comment

by:philipjonathan
ID: 24025291
Mind to show the code?
0
 

Author Comment

by:hertzgordman
ID: 24025425


Here is the SP (one just like the one above):

CREATE PROCEDURE dbo.GP_TRX_SelectTotalsForLoanTrxTypeSourceDocDateRange(@Segment_1 char(4),
@Segment_2 char(4),
@Segment_4 char(4),
@TransactionTypeCode char(10),
@StartDate datetime,
@EndDate datetime,
@SourceDoc char(11))
AS SELECT     SUM(NetTrxAmount) AS NetTrxTotal
FROM         dbo.GP_TRX_VIEW
WHERE     (Segment_1 = @Segment_1) AND (Segment_2 = @Segment_2) AND (Segment_4 = @Segment_4) AND (TransactionTypeCode = @TransactionTypeCode)
                       AND (TRXDATE BETWEEN @StartDate AND @EndDate) AND (SOURCDOC = @SourceDoc)

GO
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 24025500
Mm, I mean the code inside GetInterestReceived method. I suspect the Convert.ToDouble should go in there
0
 

Author Comment

by:hertzgordman
ID: 24025526


MyReturn = Convert.ToDouble(this.GP_TRX_SelectTotalsForLoanTrxTypeSourceDocDateRange(
"1O2", "9050",  "9205", "INTREC",PeriodStart, PeriodEnd,"CMTRX"));
 
0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 2000 total points
ID: 24025578
It calls the SP directly? I'm afraid you can't do that, you'd need to use SqlConnection and SqlCommand objects to do that. Try this:

string connectionString = "your connection string here ...";
SqlConnection connection = new SqlConnection(connectionString);
try
{
  connection.Open();
 
  string cmdText = "GP_TRX_SelectTotalsForLoanTrxTypeSourceDocDateRange";
  SqlCommand cmd = new SqlCommand(cmdText, connection);
  cmd.CommandType = CommandType.StoredProcedure;
  cmd.Parameters.Add("@Segment_1", SqlDbType.Char, 4).Value = "1O2";
  cmd.Parameters.Add("@Segment_2", SqlDbType.Char, 4).Value = "9050";
  cmd.Parameters.Add("@Segment_4", SqlDbType.Char, 4).Value = "9205";
  cmd.Parameters.Add("@TransactionTypeCode", SqlDbType.Char, 10).Value = "INTREC";
  cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = PeriodStart;
  cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = PeriodEnd;
  cmd.Parameters.Add("@SourceDoc", SqlDbType.Char, 11).Value = "CMTRX";
 
  object total = cmd.ExecuteScalar();
  return Convert.ToDouble(total);
}
finally
{
  connection.Close();
}

Open in new window

0
 

Author Closing Comment

by:hertzgordman
ID: 31564585
That's what I eventually did and it seemed to work.

Need verification on this.

Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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