none
Call SP in LINQ RRS feed

  • Question

  • I try to call a stored procedure in LINQ.

    web method which I try is

    [WebMethod]  
    public static string search_data(DateTime fromdate, DateTime todate, string region) 
    {
        try  
        {  
            string result = "";  
    
             TrackDataEntities1 td = new TrackDataEntities1();
    
                    List<griddataresult_Result> dq = new  
                   List<griddataresult_Result>();
                   dq = td.griddataresult(fromdate, todate, region).ToList();
    
            DataTable dt = new DataTable();  
    
            dt.Columns.Add("ID", typeof(int));  
            dt.Columns.Add("Owner", typeof(string));  
    
    
            foreach (var c in dq)  
            {  
                dt.Rows.Add(c.ID, c.owner);  
            }  
    
            result = DataSetToJSON(dt);  
            return result;  
        }  
        catch (Exception)  
        {  
            throw new Exception();  
        }  
     }   
    
     public static string DataSetToJSON(DataTable dt)  
     {  
         Dictionary<string, object> dict = new Dictionary<string, object>();  
         object[] arr = new object[dt.Rows.Count + 1];  
    
         for (int i = 0; i <= dt.Rows.Count - 1; i++)  
         {  
             arr[i] = dt.Rows[i].ItemArray;  
         }  
    
         dict.Add("response", arr);  
    
         JavaScriptSerializer json = new JavaScriptSerializer();  
         return json.Serialize(dict);         
    }

    check image 

    my sp return return 42 rows but when i run this code show me 577 row in table.. whereas there is not any error


    Monday, July 25, 2016 6:49 AM

All replies

  • Hi Rebmaamberrebmaamber,

    It looks like your ts.griddataresult method returns a collection of nullable int?, but you are specifying your type 'T' to be of class tblRegion_Uni.

    In order for this exception to be corrected, you would either need to change the list type to int?, or change the return type from your method griddataresult.

    The reason its throwing exception is due to the type difference, and the compiler's inability to convert the types from what you are getting versus what you are expecting.

    Best of luck.

    Monday, July 25, 2016 12:10 PM
  • You can tell a primitive type of Int that it's going to be  a  type of 'chart_project.Ts1' .
    Monday, July 25, 2016 2:01 PM
  • Hi rebmaamberrebmaamber,

    Thank you for posting here.

    I couldn't understand the LINQ. Since your issue is related to LINQ Server. So I will move the thread to LINQ to SQL forum,


    The Visual C# forum discuss and ask questions about the C# programming language,
    IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Hart


    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.

    Tuesday, July 26, 2016 2:21 AM
  • my sp return 24 rows.. and there is join in sp query so how i modify this 

    Tuesday, July 26, 2016 4:53 AM
  • @DPCodesalot please check update question

    Tuesday, July 26, 2016 6:07 AM
  • Hi rebmaamberrebmaamber,

    Based on your description, I'm not sure what could the issue, could you please provide a bit more information (such as stored procedure, related records, or a simple demo about issue.) which we could reproduce your issue on our side and try to find a solution to resolve it.

    Best regards,

    Cole Wu


    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.

    Wednesday, July 27, 2016 2:54 AM
    Moderator
  • Hi rebmaamberrebmaamber,

    I didn't really see anything that would cause the extra records from your code. Basically you are just going through the Entity Framework context object and calling the stored procedure with the provided parameters. I assume you get 42 records when you run the stored procedure locally, and when you try it via entity framework you get more records. Have you tried an ADO.NET implementation yet?

    I'm guessing the ADO.NET implementation would still yield the same result. My guess is that you might be passing in different parameter values between what you use within SSMS to get 42, and what you are passing in through this method to get 577.

    I just don't see how calling it this way would make any difference, unless you're passing in different criteria.

    Best of luck.

    Thursday, July 28, 2016 12:44 PM