Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download database SQL from here.
As you have said that your page is opened all the time then here i have used SetInterval as the timer to check the time whether it is equal to 10 AM or not. If you use Ajax Timer then page will postback each 1 seconds. So i have used SetInterval.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
var inter;
window.onload = function () {
inter = setInterval(CheckTime, 1000);
}
function CheckTime() {
var hours = new Date().getHours();
var hours = (hours + 24) % 24;
var mid = 'AM';
if (hours == 0) { //At 00 hours we need to show 12 am
hours = 12;
}
else if (hours > 12) {
hours = hours % 12;
mid = 'PM';
}
var currentTime = hours + ' ' + mid;
if (currentTime == "9 AM") {
$('[id*=GridView1] tr:not(:first)').each(function () {
var customer = {};
customer.CustomerId = $(this).find("td:first").html();
customer.Name = $(this).find("td:nth-child(2)").html();
customer.Country = $(this).find("td:last").html();
$.ajax({
type: "POST",
url: "Default.aspx/SaveUser",
data: '{customer: ' + JSON.stringify(customer) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
}
});
});
clearInterval(inter);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Select * FROM Customers", con))
{
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
}
}
public class Customer
{
public string CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
[System.Web.Services.WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod]
public static void SaveUser(Customer customer)
{
if (HttpContext.Current.Session["Saved"] == null)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers VALUES(@CustomerId, @Name, @Country)"))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@CustomerId", customer.CustomerId);
cmd.Parameters.AddWithValue("@Name", customer.Name);
cmd.Parameters.AddWithValue("@Country", customer.Country);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
HttpContext.Current.Session["Saved"] = true;
HttpContext.Current.Session.Timeout = 60;
}
}
}
}