none
ExecuteStoreQuery Casting Problem RRS feed

  • Question

  •  public IEnumerable<int> GetPopularCourseIds()
            {
     return  var dd = db.ExecuteStoreQuery<int>("select courseid from UserLessonProgresses where courseid is not null", null).ToList();
            }

    in above method bellow error is generated:-

    {"The cast to value type 'Int32' failed because the materialized value is null.
    Either the result type's generic parameter or the query must use a nullable type."}

    iany one have a idea for this? give me a proper soutions..

    • Moved by Caillen Thursday, November 14, 2013 9:34 AM Data Platform question.
    Wednesday, November 13, 2013 12:25 PM

Answers

  • Hi,

    What is the data Type of "coursed" in your table. What is the result you get when you execute the given query in sql it self. Try to execute following query first & see whether it works.

    select Top 1 courseid from UserLessonProgresses where courseid is not nul

    Thank You,

    Sammani

    http://sammanipalansuriya.blogspot.com/

    Thursday, November 14, 2013 10:09 AM
  • Hi,

    I made a smaple, please have a look at below:

    internal void Execute()
            {
                using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
                {
                    IEnumerable<int> result = GetPopularCourseIds(db);
                }
            }
    
            public IEnumerable<int> GetPopularCourseIds(DataBaseFirstDBEntities db)
            {
                ObjectContext ObjectContext = ((IObjectContextAdapter)db).ObjectContext;
    
                return ObjectContext.ExecuteStoreQuery<int>("select orderid from [Order] where orderid is not null", null).ToList();
            }

    The Order table structure:

    CREATE TABLE [dbo].[Order] (
        [OrderID]   INT           NOT NULL,
        [OrderCode] NVARCHAR (50) NOT NULL,
        [OrderName] NVARCHAR (50) NOT NULL,
        PRIMARY KEY CLUSTERED ([OrderID] ASC)
    );
    

    It works fine.

    Could you please share your table structure so that we could help you better?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 15, 2013 2:43 AM
    Moderator

All replies

  • Hello

    It means that your "selected query" return NULL and .NET cannot convert NULL -> int.

    You need write:

     

    public IEnumerable<int> GetPopularCourseIds() { return db.ExecuteStoreQuery<int?>( "select courseid from UserLessonProgresses where courseid is not null", null).ToList(); } // or public IEnumerable<int> GetPopularCourseIds() { return db.ExecuteStoreQuery<int>( "select courseid from UserLessonProgresses where courseid is not null", null)

    .Select(i => (i == null) ? 0 : Convert.ToInt32(i)).ToList(); }



    • Edited by Eugene ltd. _ Wednesday, November 13, 2013 12:37 PM
    • Proposed as answer by RohitArora Thursday, November 14, 2013 5:29 AM
    • Unproposed as answer by RohitArora Thursday, November 14, 2013 5:30 AM
    Wednesday, November 13, 2013 12:37 PM
  • its not working...
    Wednesday, November 13, 2013 12:50 PM
  • Try this

    public IEnumerable<int> GetPopularCourseIds()
            {
    List<int>courseList;
     return  var dd = db.ExecuteStoreQuery<int>("select courseid from UserLessonProgresses where courseid is
     not null","courseList", System.Data.Objects.MergeOption.AppendOnly)ToList():}

    Wednesday, November 13, 2013 1:18 PM
  • hey its again not work :-

    List<int>courseList;--------->The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

    Error is generate again plz give me solutions with proper...

    Thursday, November 14, 2013 5:11 AM
  • Hi vikaslalkiya,

    I'm moving your question to ADO.NET Entity Framework and LINQ to Entities forum where you could receive more efficient responses.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 14, 2013 9:36 AM
  • Hi,

    What is the data Type of "coursed" in your table. What is the result you get when you execute the given query in sql it self. Try to execute following query first & see whether it works.

    select Top 1 courseid from UserLessonProgresses where courseid is not nul

    Thank You,

    Sammani

    http://sammanipalansuriya.blogspot.com/

    Thursday, November 14, 2013 10:09 AM
  • Hi,

    I made a smaple, please have a look at below:

    internal void Execute()
            {
                using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
                {
                    IEnumerable<int> result = GetPopularCourseIds(db);
                }
            }
    
            public IEnumerable<int> GetPopularCourseIds(DataBaseFirstDBEntities db)
            {
                ObjectContext ObjectContext = ((IObjectContextAdapter)db).ObjectContext;
    
                return ObjectContext.ExecuteStoreQuery<int>("select orderid from [Order] where orderid is not null", null).ToList();
            }

    The Order table structure:

    CREATE TABLE [dbo].[Order] (
        [OrderID]   INT           NOT NULL,
        [OrderCode] NVARCHAR (50) NOT NULL,
        [OrderName] NVARCHAR (50) NOT NULL,
        PRIMARY KEY CLUSTERED ([OrderID] ASC)
    );
    

    It works fine.

    Could you please share your table structure so that we could help you better?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 15, 2013 2:43 AM
    Moderator