0

I have 2 tables.

Branches (BranchId,Name, Area)
Managers (ManagerId,Name,BranchId)

I want when I click on Branches branch data will show with manager name. I use gridview to show data.

<asp:GridView runat="server" ClientIDMode="Static" ID="gvBranches">
</asp:GridView>

First of all I write LINQ Query.

CDDataContext dc = new CDDataContext();
var data = (from branch in dc.Branches
            join manager in dc.Managers
            on branch.BranchId equals manager.BranchId
            select branch).ToList();
gvBranches.DataSource = data;
gvBranches.DataBind();

It just display the record of branch. like BranchId Name Area

Then I write Stored Procedure.

Stored Procedure:

create procedure sp_AllBranches
As
Begin
return  Select b.BranchId, b.Name, b.Area, m.Name
        from Branches as b, Managers as m
        where b.BranchId=m.BranchId
End

Code:

CDDataContext dc = new CDDataContext();
var data = dc.sp_AllBranches();
gvBranches.DataSource = data;
gvBranches.DataBind();

It display nothing. Please help.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Salman Mushtaq
  • 341
  • 4
  • 23
  • 1
    You need to import the stored procedure as a `Function`. Check this post for complete solution: http://stackoverflow.com/questions/32140774/getting-data-from-stored-procedure-with-entity-framework – Salah Akbari Feb 19 '16 at 11:23
  • @user2946329 I read above link, I didn't use Entity framework and there is no option to import Stored Procedure as a Functions. Simply I just want to fill `GridView` with results that come from two tables. Also I execute stored procedure it returns 0. – Salman Mushtaq Feb 19 '16 at 11:34
  • I find another article on stackoverflow, it solves my problem with LINQ query. `CDDataContext dc = new CDDataContext(); var data = from branch in dc.Branches join manager in dc.Managers on branch.BranchId equals manager.BranchId select new{ branch.BranchId, BranchName = branch.Name, branch.Area, ManagerName = manager.Name } ; gvBranches.DataSource = data; gvBranches.DataBind(); }` – Salman Mushtaq Feb 19 '16 at 11:51
  • Here is the link [link](http://stackoverflow.com/questions/18978395/an-anonymous-type-cannot-have-multiple-properties-with-the-same-name/18983808#18983808) – Salman Mushtaq Feb 19 '16 at 11:54
  • If there is a relationship between branch and manager there should be a foreign key in you database and this should be represented by your model. So Manager should have a Branch property and Branch a Managers property. You shouldn't need to join two entities with a relationship. – Mant101 Feb 19 '16 at 13:03

0 Answers0