Solved

Problem Using Stored Procedures

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

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

20 Experts available now in Live!

Get 1:1 Help Now