I am using stored procedure to load data in my gridview. Based on user selection, He /she should be able to view the data in asp.net gridview either the itemized , detailed sales figure of all tenants in one location OR the overall sum total sales of that location.
This would mean changing of field column.
For PER DETAILED:
tenantcode tenantname, location, Gross sales

For TOTAL per location:
Location, Gross sales (using group by clause to achieve this)

IN STORED PROCEDURE , everything works fine of course, but in asp.net, per detailed option is working, but when I choose the next option which is over all sales per location it produces this error:
A field or property with the name 'tenantcode' was not found on the selected data source.
Markup:
<asp:GridView ID="grdMarketingReport1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" Font-Size="Smaller" EmptyDataText="No Records Found" ShowHeaderWhenEmpty="True" Width="100%" AutoGenerateColumns="false">
<EmptyDataRowStyle BackColor="white" ForeColor="black" />
<EmptyDataTemplate>No Data Found.</EmptyDataTemplate>
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="RP Code" DataField="tenantcode" />
<asp:BoundField HeaderText="Retail Partner" DataField="name" />
<asp:BoundField HeaderText="Location" DataField="locationd" />
<asp:BoundField HeaderText="Gross Sales" DataField="gs" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" Height="25px" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
I am actually thinking of creating two different gridview to cater my needs, but that's the least option unless it is the only way. COULD IT BE DONE IN ONE GRIDVIEW ONLY?
STORED PROC (PART OF THE ACTUAL SP)
if (@RP = 'ALL' and @Location = 'ALL' and @Business = 'ALL')
begin
--step 1: get the sales per date
SELECT a.tenantcode , b.name , a.location , c.locationd , d.[desc] as 'Business' , a.date , a.gsc, b.sdate , b.cdate
into #Sample5
FROM DAILYMOD a INNER JOIN TENANT b on a.tenantcode = b.tenantcode
INNER JOIN LOCATION c on b.location = c.location
INNER JOIN BUSINESS d on b.business = d.code
WHERE ((a.date between @DateFrom1 and @DateTo1 )
or (a.date between @DateFrom2 and @DateTo2 )
or (a.date between @dateFromLY and @dateToLY))
ORDER BY a.location , a.tenantcode , a.date
--step 2: group the gsc per date ranges
select tenantcode , name, location, locationd, business , sdate, cdate,
SUM(case when date between @DateFrom1 and @DateTo1 then [gsc] else 0 end) 'date1',
SUM(case when date between @DateFrom2 and @DateTo2 then [gsc] else 0 end) 'date2',
SUM(case when date between @dateFromLY and @dateToLY then [gsc] else 0 end) 'dateLY'
from #Sample5
GROUP BY tenantcode , name, location, locationd , business, sdate, cdate
end
else
begin
--step 1: get the sales per date
SELECT a.tenantcode , b.name , a.location , c.locationd , d.[desc] as 'Business' , a.date , a.gsc, b.sdate , b.cdate
into #Sample7
FROM DAILYMOD a INNER JOIN TENANT b on a.tenantcode = b.tenantcode
INNER JOIN LOCATION c on b.location = c.location
INNER JOIN BUSINESS d on b.business = d.code
WHERE ((a.date between @DateFrom1 and @DateTo1 )
or (a.date between @DateFrom2 and @DateTo2 )
or (a.date between @dateFromLY and @dateToLY ))
ORDER BY a.location , a.tenantcode , a.date
--step 2: group the gsc per date ranges
select location, locationd,
SUM(case when date between @DateFrom1 and @DateTo1 then [gsc] else 0 end) 'date1',
SUM(case when date between @DateFrom2 and @DateTo2 then [gsc] else 0 end) 'date2',
SUM(case when date between @dateFromLY and @dateToLY then [gsc] else 0 end) 'dateLY'
from #Sample7
GROUP BY location, locationd
end