locked
how to use a SQL stored procedure in a Object Data Source??? RRS feed

  • Question

  • User172691333 posted

    Hi,

    I have created a stored querry and want to use it as the update method of my data source object.

    Itried the following:

    I created a class and wrote a function in that class (using the stored querry), so I can later use that function in my Data source object. But iget an error.

    Here is the code and the error I get:

    .

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Web;
    
    
    namespace ElogObjectBoundingTest
    
    {
    
    
    public class Bal
    
        {
    
          public Logbook getLogbookIDs1()
    
            {
    
        using (var context = new APSELOGDataContext())
    
                {             
    
           return context.sp_GetLogbooks();
    
                }
    
            }
    
        }
    
    }
    

     But I get the following error:

    Error 1 Cannot implicitly convert type 'System.Data.Linq.ISingleResult<ElogObjectBoundingTest.Logbook>' to 'ElogObjectBoundingTest.Logbook'. An explicit conversion exists (are you missing a cast?) C:\Users\ACER\Documents\Visual Studio 2010\Projects\APS\ElogApp\ElogObjectBoundingTest\ElogObjectBoundingTest\Bal.cs 42 24 ElogObjectBoundingTest

    
    

    Any help is appreciated.

    Thursday, December 10, 2015 4:18 AM

Answers

  • User1724605321 posted

    Hi kia ,

    Welcome to ASP.NET Forum .

    Since you use a stored procedure in LINQ, it'll generate a class T(procedure name + "Result") for you automatically to store the data. The returned type is always ISingleResult<T>.In your code, you need to use ISingleResult<T> to receive your data retrieved from the stored procedure. ISingleResult<T> can't be converted to "Logbook" type  and caused the problem. Here's the sample code:

    ISinlgeResult<Logbook> books= context.sp_GetLogbooks();
     foreach(Logbook value in books)
     {...}

    For more details ,please refer to below article:

    http://weblogs.asp.net/scottgu/linq-to-sql-part-6-retrieving-data-using-stored-procedures .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 10, 2015 6:33 AM