Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Class.    Feetype
I.           Jan
I.           Feb
I.           Mar
II.          Jan

This is my table data. How can I use Feetype on pivot
VB
Dim VFeeType As String = "SELECT COALESCE([' + FeeType + ']‘) FROM FeeMaster1"  
Dim strSQL6 As String = "select * from (select Regno, FeeType,SName, AmountPaid,Class from FeeTrans)FeeTrans PIVOT(Sum(AmountPaid) for [FeeType] in (' + VFeeType + ')) as AP where Class='" & cboClass.Text & "'"
        Dim DaAp6 As New SqlDataAdapter(strSQL6, con)
        Dim Dset6 As New DataTable
        DaAp6.Fill(Dset6)
        DataGridView2.DataSource = Dset6
Posted
Updated 1-Feb-13 9:06am
v2
Comments
Sandeep Mewara 1-Feb-13 15:09pm    
1. What error?
2. You need to be clear on what output format you are expecting data
3. Is PIVOT really needed?
Navas Khanj 2-Feb-13 1:08am    
My Error Is : i declar PIVOT code in (VFeeType)that one get data from table.
my DGV Want Display
Regno SName Class (FeeType Data(VFeeType)) For Example

Regno SName Class Jan Feb Mar
how can i get (Jan Feb Mar , etc..) TableFiled want show to DGV.. Please tell me some idea how can i get..

1 solution

First of all, using SELECT * in pivot table is wrong! Pivot tables need to use at least one column to produce rows and at least one (or more) column that produced column-headers.
More about pivots here:
http://msdn.microsoft.com/en-US/library/ms177410%28v=sql.105%29.aspx[^]
http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=PIVOTData[^]

Secondly, NEVER do it in a code! Use stored procedure on a server side.
More about stored procedures:
http://msdn.microsoft.com/en-us/library/ms190669(v=sql.105).aspx[^]
http://msdn.microsoft.com/en-us/library/ms187961.aspx[^]

Probably, your stored procedure should looks like:
SQL
CREATE PROCEDURE GetMyPivot()
    @class NVARCHAR(20)

AS
BEGIN

DECLARE @cols NVARCAHR(200)
			
SET @col = STUFF((SELECT DISTINCT '],[' + [FeeType] 
                 FROM FeeMaster1
                 ORDER BY '],[' + [FeeType]
                 FOR XML PATH('')),1,2,'') + ']' 

SELECT [Regno], [SName], [Class], @cols
FROM (
      SELECT [Regno], [FeeType], [SName], [AmountPaid], [Class] 
      FROM FeeTrans
     ) AS DT
PIVOT(SUM([AmountPaid]) FOR [FeeType] IN (@cols)) as PT 
WHERE [Class]=@class 

END
 
Share this answer
 
Comments
Navas Khanj 14-Feb-13 12:04pm    
Thanks
Maciej Los 14-Feb-13 12:09pm    
You're welcome ;)
If my answer was helpful, please, rate it and mark as solved.

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