Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hello everyone,

I have values in database like this:
C#
Employee name      Date And Time

Xing Zuber         2013-07-13 09:10 PM
Daving solman      2013-07-13 09:08 PM
Iyan Yokovich      2013-07-12 09:00 PM 
Dev richardson     2013-07-03 09:11 PM
Xing Zuber         2013-07-03 09:16 PM
Xing Zuber         2013-07-11 09:02 PM
Dev richardson     2013-07-11 09:15 PM
.......................................

I have same structure in database,and i am able to display this in gridview(Same as shown in above sample),that's fine.

I need to display it like below.
C#
Name            2013-07-13   ...2013-07-11    ...2013-07-03  ..........
Xing Zuber      09:10 PM       09:02 PM        09:16 PM      ..........
Dev richardson  ........       09:15 PM        09:11 PM      ..........

My question is,

1) Which is the appropriate control to use in such scenario?

2) What approach should i look at in order to get this?

I will have two calenders at top of the page to select dates. When user selects the date from it,data needs to be displayed between those dates.

Any help would be really appreciated.

Regards..
Posted
Updated 11-Aug-13 20:34pm
v2

1 solution

Steps to do:
1) Create Stored Procedure (SP)[^]
2) Copy And Paste below code into the body of SP:
SQL
CREATE TABLE #EmpInput (EmpName VARCHAR(30), DaTi SMALLDATETIME)

INSERT INTO #EmpInput (EmpName, DaTi)
SELECT 'Xing Zuber', '2013-07-13 09:10 PM'
UNION ALL SELECT 'Daving solman', '2013-07-13 09:08 PM'
UNION ALL SELECT 'Iyan Yokovich', '2013-07-12 09:00 PM' 
UNION ALL SELECT 'Dev richardson', '2013-07-03 09:11 PM'
UNION ALL SELECT 'Xing Zuber', '2013-07-03 09:16 PM'
UNION ALL SELECT 'Xing Zuber', '2013-07-11 09:02 PM'
UNION ALL SELECT 'Dev richardson', '2013-07-11 09:15 PM'

DECLARE @cols VARCHAR(300)
DECLARE @dt VARCHAR(1000)
DECLARE @pt VARCHAR(4000)

SET @cols = STUFF((SELECT DISTINCT '],[' + CONVERT(VARCHAR(10),DaTi,121)
					FROM #EmpInput
					ORDER BY  '],[' + CONVERT(VARCHAR(10),DaTi,121)
				FOR XML PATH('')),1,2,'') + ']'
--SELECT @cols AS Cols

SET @dt = N'SELECT EmpName, CONVERT(VARCHAR(10),DaTi,121) AS IDate, CONVERT(VARCHAR(10),DaTi,108) AS ITime
		FROM #EmpInput'
--EXEC(@dt)

SET @pt = N'SELECT EmpName, ' + @cols + ' ' +
		'FROM (' + @dt + ') AS DT ' + 
		'PIVOT(MAX(ITime) FOR IDate IN(' + @cols + ')) AS PT '
EXEC(@pt)


DROP TABLE #EmpInput

3) Change the code to your needs
4) Call SP from ASP.NET code behind[^] and bind result to a gridview ;)

End ;)
 
Share this answer
 
Comments
Coder_one 12-Aug-13 3:10am    
Thanks Maciej.Its just just amazing. I am trying to do this since almost 13 days. This worked exactly what i wanted. But there are last two problems remaining.

1) I am calling this stored procedure like below:

SqlConnection con = new SqlConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connection name].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("SQLQuery1",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
This is giving me error that,SQLQuery1 doesnt exist?

2) You have mentioned data with names etc.
INSERT INTO #EmpInput (EmpName, DaTi)
SELECT 'Xing Zuber', '2013-07-13 09:10 PM'....
What if i have two columns named ename and datetime_?

You have mentioned data to be displayed in query itself.Can you help me modify this query something that fits for any data with the above mentioned two columns?
Maciej Los 12-Aug-13 3:18am    
ad 1) you need to set cm.Text = "TheNameOfStoredProcedure". Is your stored procedure named SQLQuery1? Have you created SP?
ad 2) #EmpInput is a temporary table. I belive you have your own table. You don't need to create temporary table. I've done in only for example. Replace #EmpInput with the name of your (existing) table. Raplace column name EmpName with ename and DaTi with datetime_.

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