In this blog we will know how to display Intime and Outtime
of employee in a Gridview.Suppose we have a table named as employee which
contains four columns as empCode, inoutMode, date and time. In time column we
have both the Intime and Outtime of the employee respectively. So, here our
work is to display both the Intime and Outtime separately of the respective
employees into a Gridview.
The scenario is
Create table employee (empCode varchar(50),inoutMode
int,date varchar(50),time varchar(50))
employee table data
empCode inoutMode date time
6001 0 16/09/2011
09:05:34
6002 0 16/09/2011
09:33:13
6001
1 16/09/2011 18:05:09
6002 1 16/09/2011
17:44:34
I need the output as below
empCode date Intime Outtime
6001 16/09/2011 09:05:34 18:05:09
6002
16/09/2011 09:33:13 17:44:34
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
</div>
<asp:GridView ID="GridView1"
runat="server">
</asp:GridView>
</form>
</body>
</html>
Default.aspx.cs
code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial
class _Default
: System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object
sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "SELECT
a.empCode,a.date,a.[time] AS Intime,b.Outtime FROM employee A LEFT JOIN (
SELECT empCode,date,[time] AS Outtime FROM
employee WHERE
employee.inoutMode = 1 ) b ON
a.empCode = b.empCode AND a.date = b.date WHERE a.inoutMode = 0";
com = new SqlCommand(str, con);
SqlDataReader reader =
com.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
con.Close();
}
}
Thanks for reading.