Its my asp.net c# coding.
public void GridTime()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("username", typeof(string)), new DataColumn("intime", typeof(DateTime)), new DataColumn("Outtime", typeof(DateTime)),new DataColumn("totaltime",typeof(DateTime))});
string Sql = "Select *From timesheet";
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SUJAY;Data Source=SUPREME6-PC");
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter(Sql, conn);
adp.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
foreach (GridViewRow row in this.GridView1.Rows)
{
DateTime intime = Convert.ToDateTime(dt.Rows[row.RowIndex]["Intime"], new CultureInfo("en-GB"));
DateTime outtime = Convert.ToDateTime(dt.Rows[row.RowIndex]["Outime"], new CultureInfo("en-GB"));
TimeSpan t = outtime - intime;
row.Cells[3].Text = t.ToString();
}
}
its my sql server column
username-nvarchar
intime-datetime
outime-outtime
Inserting intime and outime
protected void Button1_Click(object sender, EventArgs e)
{
DateTime time = DateTime.Parse(string.Format("{0}:{1}:{2} {3}", TimeSelector1.Hour, TimeSelector1.Minute, TimeSelector1.Second, TimeSelector1.AmPm));
var shortDate = time.Date;
ClientScript.RegisterStartupScript(this.GetType(), "time", "alert('Selected Time: " + time.ToString("hh:mm:ss tt") + "');", true);
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SUJAY;Data Source=SUPREME6-PC");
conn.Open();
String s = "Insert into timesheet(username,intime) values('" + Label1.Text + "','" + time + "')";
//String s = "Insert into timecal(starttime) values('" + time + "')";
SqlCommand cmd = new SqlCommand(s, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
DateTime time = DateTime.Parse(string.Format("{0}:{1}:{2} {3}", TimeSelector1.Hour, TimeSelector1.Minute, TimeSelector1.Second, TimeSelector1.AmPm));
var shortDate = time.Date;
ClientScript.RegisterStartupScript(this.GetType(), "time", "alert('Selected Time: " + time.ToString("hh:mm:ss tt") + "');", true);
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SUJAY;Data Source=SUPREME6-PC");
conn.Open();
String s = "Update timesheet SET outtime='" + time + "' WHERE username='"+Label1.Text+"' ";
SqlCommand cmd = new SqlCommand(s, conn);
cmd.ExecuteNonQuery();
conn.Close();
endTime = DateTime.Now;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblval1 = (Label)e.Row.FindControl("Label1");
Label lblval2 = (Label)e.Row.FindControl("Label2");
Label lblval3 = (Label)e.Row.FindControl("Label3");
lblval3.Text = Convert.ToString( Convert.ToDateTime(lblval1.Text)-Convert.ToDateTime(lblval2.Text) );
}
}