Click here to Skip to main content
15,887,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone,

I want to retrieve data from SQL database and bind to control(Datagridview / Datalist). Make a table format output and it will shows Rows as column and column as rows.

I have one table: tbl_CustData with two fields such as FixedDepDate(datetime datatype) and another is Month_year (nvarchar datatype). The values are as follows:

FixedDepDate | Month_year
2018-07-03 00:00:00.000 | March-18
2018-08-23 00:00:00.000 | August - 2018
2018-08-29 00:00:00.000 | August - 2018
2018-07-04 00:00:00.000 | July - 2018
2018-07-10 00:00:00.000 | July - 2018
2018-07-25 00:00:00.000 | July - 2018

Expected Output is :

March - 18 | 03
July - 18 | 04 | 10 | 25
August - 18 | 23 | 29



I already tried following methods but not getting output which is I am expecting.
1) Used Datalist/Gridview control and tried to bind data
2) Used PIVOTE operator but not getting output.

Also please give some idea which control is suitable to bind the data and show on my webpage or else any technique to resolve this problem.
Note: I am not using any events to show my output. Want to display my output directly on my page after run the project.

Thanking you in advance and waiting for your valuable solution.

What I have tried:

I want to Bind data from database and rotate / transpose data.
Posted
Updated 5-Jul-18 2:07am
v2

You need to check pivot which convert the value as same whateve you want on database side
 
Share this answer
 
Comments
Pankaj sonar 5-Jul-18 1:49am    
Can you make SQL query using PIVOTE operator for expected output? Please help..!!
im giving you one example you need to check at your end how to sonsolidate this things into your code

SELECT * FROM (SELECT year(invoiceDate) as [year], left(datename(month,invoicedate),3)as [month], _
InvoiceAmount as Amount FROM Invoice) as InvoiceResult

Suppose you have data with
Year month amount
2010 Jan 100
2011 Feb 90
2010 Feb 110
Then use pivot table on sql query like below

SELECT *
FROM (
SELECT
year(invoiceDate) as [year],left(datename(month,invoicedate),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS pvt

output will be

year Jan Feb
2010 100 110
2011 90

Please check at your end i hope you got help from this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900