2

C# code

public List<searchProducts_Result> GetProducts()
{
            var nameParam = new ObjectParameter("numbers", typeof(int));
            List<searchProducts_Result> listobjs = new List<searchProducts_Result>();
            ObjectResult<searchProducts_Result> objResult = null;
            searchProducts_Result outParam = new searchProducts_Result();
            using (var db = new SPWebEntities())
            {
                objResult = db.searchProducts("asd", 2, 5, "15", nameParam);
                if (nameParam.Value != null)
                    outParam.UserID = nameParam.Value.ToString();
                else
                    outParam.UserID = "0";
               listobjs.Add(outParam);
                foreach (searchProducts_Result sr in objResult)
                {
                    listobjs.Add(sr);
                }
            }

            return listobjs;
}

My stored procedure:

[searchProducts]
    @productName varchar(50),
    @pageStart int=2,
    @pageEnd int=4,
    @result varchar(MAX),
    @numbers int output
as
   select @numbers = COUNT(*)  
   from products 
   where productName like @productName 

   select *
   from (select 
            *,
            ROW_NUMBER() over (order by ID) as row 
         from products 
         where productName like @productName) a 
   where a.row between @pageStart and @pageEnd

nameParam.Value It's always return null value

When I execute the stored procedure in SQL Server Mgmt Studio, it appears that it has a value but in c# its always null

6
  • What is the search products method? Commented Aug 27, 2013 at 15:14
  • search product method generated from (ado.net entity framework) by function import Commented Aug 27, 2013 at 15:17
  • Having you tried executing your proc with db.sqlquery<Entity>? Commented Aug 27, 2013 at 15:18
  • yes and its result fine , its gave me that @numbers = 15 Commented Aug 27, 2013 at 15:24
  • but in C# its always return null Commented Aug 27, 2013 at 15:25

1 Answer 1

5

You have to enumerate your result in order to actually execute the stored procedure. Try this:

public List<searchProducts_Result> GetProducts()
{
   var nameParam = new ObjectParameter("numbers", typeof(int));
   List<searchProducts_Result> listobjs = new List<searchProducts_Result>();

   // List<searchProducts_Result> objResult = null;

   searchProducts_Result outParam = new searchProducts_Result();
   using (var db = new SPWebEntities())
   {
      // by calling ToList() you execute the SP
      List<searchProducts_Result> objResult =
          db.searchProducts("asd", 2, 5, "15", nameParam).ToList();

      if (nameParam.Value != null)
         outParam.UserID = nameParam.Value.ToString();
      else
         outParam.UserID = "0";
      listobjs.Add(outParam);

      foreach (searchProducts_Result sr in objResult)
      {
         listobjs.Add(sr);
      }
   }

   return listobjs;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.