Solved

Problem Using Stored Procedures

Posted on 2009-04-06
2
184 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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