SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers


DataContext ExecuteQuery(Of TResult) Method Unable to cast object of type OneTimeEnumerable Error


In a recent ASP.NET web site project as datasource of a datagrid / gridview object, I used a LINQ to SQL Classes object.
I created the Data Class .dbml file and the data context class DataClasses1DataContext easily.
To get data from database and set the data source of the datagrid, I used the ExecuteQuery (Of TResult) method of the DataContext class within the System.Data.Linq namespace.

DataContext class ExecuteQuery (Of TResult) method directly executes a SQL query on the database and returns the result as objects.
Although the ExecuteQuery method takes two parameters (arguments) which are the query as string and the parameters as Object() array, I do not need any parameters within the query string.
So I did not pass the parameters arguments to the ExecuteQuery (Of T) method while I was calling that DataContext class. Although it is optional it was not stated within the MSDN documentation.

By the way I used the AdventureWorks sample database for the related example asp.net code.
And build the data context using the dbo.Product table within the sample SQL Server database AdventureWorks.

Here is the sample ASP.NET code for binding data to a gridview asp.net data control object using DataContext.ExecuteQuery method :

Dim productDataContext As New DataClasses1DataContext

Dim connectionStringBuilder As New SqlConnectionStringBuilder()
' /articles/add-connection-string-to-web-configuration-file.aspx
'Using SQL Authentication
connectionStringBuilder.DataSource = "localhost"
connectionStringBuilder.UserID = "testuser"
connectionStringBuilder.Password = "TestuserPwd1"
connectionStringBuilder.InitialCatalog = "AdventureWorks"

productDataContext.Connection.ConnectionString = connectionStringBuilder.ToString
productDataContext.Connection.Open()

Dim IEnumerableList As IEnumerable(Of Product)
IEnumerableList = productDataContext.ExecuteQuery(Of Product)("Select * from Product")

Me.GridView1.DataSource = IEnumerableList
Me.GridView1.DataBind()

productDataContext.Connection.Close()
Code

In the above ASP.NET code, the most important section for fetching data from database is shown below where we define the datasource object and call the datacontext method ExecuteQuery.

Dim IEnumerableList As IEnumerable(Of Product)
IEnumerableList = productDataContext.ExecuteQuery(Of Product)("Select * from Product")
Code

Actually you can define the return object container without defining its data type, the ExecuteQuery method will handle it proparly. But if you set the object type as IEnumerable(Of Product), you will be able to use the Product Data Context properties like IEnumerableList.ToList(0).ProductID


Unable to cast object of type OneTimeEnumerable

If you fail to define the return type of the data context class ExecuteQuery method, you may get error similar to "Unable to cast object of type OneTimeEnumerable"

For example, if you set the IEnumerableList object as in type List(Of Product) instead of the correct data type form IEnumerable(Of Product), you will get the following error message :

'FALSE
Dim IEnumerableList As List(Of Product)
'CORRECT
Dim IEnumerableList As IEnumerable(Of Product)
Code

Unable to cast object of type OneTimeEnumerable

System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type 'OneTimeEnumerable`1[website1.Product]' to type 'System.Collections.Generic.List`1[website1.Product]'."
Code


Visual Studio


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.