Solved

Problem Using Stored Procedures

Posted on 2009-04-06
2
186 Views
Last Modified: 2013-11-26
I'm having a problem using a stored procedure when trying to get all rows of a table.

I have a property (see code snippet) named "AllRows" which returns a strongly typed DataTable table of all rows in a SQL mdf DB.
When I execute, an Exception is caught and the message says that:
            LastError      "Unable to cast object of type 'System.Data.DataTable' to type 'DestinationDataTable'."

Stored procedure is also attached in code snippet.

Thanks, and Any help on this topic would be most appreciated.
JJW
''' <summary>
    ''' All rows in a data table
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property AllRows() As TravelDataSet.DestinationDataTable
        Get
            Try
                Return DirectCast(Utils.GetTable("spAllDestinations", CommandType.StoredProcedure), TravelDataSet.DestinationDataTable)
            Catch ex As Exception
                LastError = ex.Message
                Return Nothing
            End Try
        End Get
    End Property
 
 
    ''' <summary>
    ''' All rows in a data table
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property AllRows() As TravelDataSet.DestinationDataTable
        Get
            Try
                Return DirectCast(Utils.GetTable("spAllDestinations", CommandType.StoredProcedure), TravelDataSet.DestinationDataTable)
            Catch ex As Exception
                LastError = ex.Message
                Return Nothing
            End Try
        End Get
    End Property
 
 
 
Stored Procedure:
ALTER PROCEDURE dbo.spAllDestinations
AS
	SET NOCOUNT ON;
	SELECT	DestinationId, 
		Destination,
		Address,
		City, 
		State,
		Zip,
		PhoneNumber,
		DailyCost,
		DepositPctRequired,
		RoomCapacity 
	FROM	[Destination]

Open in new window

0
Comment
Question by:jackwebb22002
2 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 500 total points
ID: 24083572
have a look at this:  http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/c085e126-694a-4224-bef8-41b8ae69d7db/    

It is not possible to cast a datatable to a typed datatable object.  The link mentions using a merge to transfer the data into the typed datatable.
0
 

Author Closing Comment

by:jackwebb22002
ID: 31567349
Thanks very much.  I can easily get by with the Getdata method of the dataset's TableAdapter, but needed confirmation that it's not ordinarily possible to do this without something like the merge mentioned in the link.

Now I know I can proceed, with all SP's for other functions, and use the Getdata to retrieve an entire table.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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