I am getting Execption when I use the following code.
Code 1: Employee.cs File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace SqlDatabaseCreation
{
public partial class Movie
{
public static IEnumerable<Movie>Sjelect()
{
MovieDetailsDataContext mdb = new MovieDetailsDataContext();
return mdb.MovieDetails.OrderBy(e => e.Director);
Execption ::(Cannot implicitly convert type System.Linq.IOrderedQueryable<SqlDatabaseCreation.MovieDetail> to System.Collections.Generic.IEnumerable<SqlDatabaseCreation.Movie>. An explicit conversion exists (are you missing a cast))
I believe as Careen mentioned - this is an issue of possibly using the wrong context (or class). Your DataSource is expecting an IEnumerable<Movie> but you are passing it an IEnumerable<MovieDetails> :
bibeksoft
0 Points
1 Post
Cannot Convert
Feb 03, 2013 11:07 AM|LINK
I am getting Execption when I use the following code.
Code 1: Employee.cs File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace SqlDatabaseCreation
{
public partial class Movie
{
public static IEnumerable<Movie>Sjelect()
{
MovieDetailsDataContext mdb = new MovieDetailsDataContext();
return mdb.MovieDetails.OrderBy(e => e.Director);
}
}
}
Code 2:Show.aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OBJECT DATA SOURCE</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GRD" runat="server" DataSourceID="SRM" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" EnableModelValidation="True">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="SRM" runat="server"
TypeName="Movie" SelectMethod="Select"></asp:ObjectDataSource>
</div>
</form>
</body>
</html>
Execption ::(Cannot implicitly convert type System.Linq.IOrderedQueryable<SqlDatabaseCreation.MovieDetail> to System.Collections.Generic.IEnumerable<SqlDatabaseCreation.Movie>. An explicit conversion exists (are you missing a cast))
Careed
All-Star
18774 Points
3637 Posts
Re: Cannot Convert
Feb 03, 2013 12:40 PM|LINK
Change your Select method from returning IEnumerable<Movie> to IEnumerable<MovieDetail>, especially since that's what you're querying.
"The oxen are slow, but the earth is patient."
Nilu87
Member
166 Points
29 Posts
Re: Cannot Convert
Feb 03, 2013 01:00 PM|LINK
You can also return list
public static List<MovieDetails>Sjelect() { MovieDetailsDataContext mdb = new MovieDetailsDataContext(); return mdb.MovieDetails.OrderBy(e => e.Director); }Rion William...
All-Star
27896 Points
4618 Posts
Re: Cannot Convert
Feb 03, 2013 02:28 PM|LINK
I believe as Careen mentioned - this is an issue of possibly using the wrong context (or class). Your DataSource is expecting an IEnumerable<Movie> but you are passing it an IEnumerable<MovieDetails> :
<asp:ObjectDataSource ID="SRM" runat="server" TypeName="Movie" SelectMethod="Select"></asp:ObjectDataSource> </div>You will either need to change the type that your DataSource is expecting to MovieDetails :
<asp:ObjectDataSource ID="SRM" runat="server" TypeName="MovieDetail" SelectMethod="Select"></asp:ObjectDataSource> </div>or you can use a different DataContext to return an IEnumerable<Movie>
//Example code public static IEnumerable<Movie> Select() { MovieDataContext mdb = new MovieDataContext(); return mdb.Movies.OrderBy(e => e.Director); }