locked
Linq to SQL and Stored Procedure RRS feed

  • Question

  • User-305496339 posted

      I am using Linq to SQL. Below is the code I am using. As you can see I am using a stored procedure and passing it an integer but yet q, the inferred type variable contains nothing after this code is executed.  Thanks for any help !

    DataClasses1DataContext db = new DataClasses1DataContext();
    var q = db.spGetEmployeeById(3);

    Monday, September 25, 2017 8:24 PM

Answers

  • User-1838255255 posted

    Hi rkrex,

    The problem maybe is that the LINQ Data Layer (.dbml) inside VS doesn't recognize that the SP returns something and therefore I can't get the return value in my code.

    please check the following tutorials about this issue:

    http://msdn.microsoft.com/en-us/library/bb918119.aspx 

    https://forums.asp.net/t/1992214.aspx?Procedure+returning+value+but+code+returning+null

    From the code you provided, I suggest that you could check your database to make sure that when you give parameter '3' whether you have any data in database.

    If you still have problem, please show me the data in database and your store procedure for better reproducing.

     

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 26, 2017 11:22 AM
  • User1655654435 posted

    did it work? It does not seem logical to me that you get a list when asking for an id number. So FirstOrDefault(), Find(), SingleOrDefault(), would be more natural. Also, you might want to specify the return type in the variable. Like instead of using var, use the type that is returned(entityframework might not know what type is returned by the stored procedure call)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 27, 2017 8:10 AM

All replies

  • User-1838255255 posted

    Hi rkrex,

    The problem maybe is that the LINQ Data Layer (.dbml) inside VS doesn't recognize that the SP returns something and therefore I can't get the return value in my code.

    please check the following tutorials about this issue:

    http://msdn.microsoft.com/en-us/library/bb918119.aspx 

    https://forums.asp.net/t/1992214.aspx?Procedure+returning+value+but+code+returning+null

    From the code you provided, I suggest that you could check your database to make sure that when you give parameter '3' whether you have any data in database.

    If you still have problem, please show me the data in database and your store procedure for better reproducing.

     

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 26, 2017 11:22 AM
  • User-305496339 posted

               DataClasses1DataContext db = new DataClasses1DataContext();
               var q = db.spGetEmployeeById(1).ToList();

    Tuesday, September 26, 2017 3:44 PM
  • User1655654435 posted

    did it work? It does not seem logical to me that you get a list when asking for an id number. So FirstOrDefault(), Find(), SingleOrDefault(), would be more natural. Also, you might want to specify the return type in the variable. Like instead of using var, use the type that is returned(entityframework might not know what type is returned by the stored procedure call)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 27, 2017 8:10 AM
  • User-305496339 posted

    Sorry for the delay in replying back to your post. Yes it does work. And I will explore your other suggestions. Thanks for your help !

    Thursday, October 12, 2017 6:22 PM