Solved

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

Posted on 2009-03-30
9
1,124 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
defining NULL or 0 10 44
Need a simple RegEx to search for two words 27 49
asp Google Map 2 33
How to use NFS (Network File System) in Asp.net mvc 5? 4 45
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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