Hi alhamd,
Refer below sample.
Database
CREATE TABLE tblNurs
(
ID INT,
No INT,
Name VARCHAR(50),
Is_Active BIT
)
GO
INSERT INTO tblNurs VALUES (1, 123, 'ABC', 'TRUE');
INSERT INTO tblNurs VALUES (2, 234, 'SKY', 'TRUE');
INSERT INTO tblNurs VALUES (3, 456, 'XYZ', 'TRUE');
INSERT INTO tblNurs VALUES (4, 567, 'PQR', 'FALSE');
HTML
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime" runat="server" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Timer ID="Timer1" runat="server" OnTick="TimerTick" Interval="1000" />
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
lblTime.Text = "Last Refreshed: " + DateTime.Now.ToString();
DataTable dt = GetData();
DataTable dtFinal = dt.Clone();
if (i < dt.Rows.Count)
{
dtFinal.Rows.Add(dt.Rows[i].ItemArray);
GridView1.DataSource = dtFinal;
GridView1.DataBind();
}
}
protected void TimerTick(object sender, EventArgs e)
{
if (this.GetData().Rows.Count != i)
{
UpdatePatient(this.GetData().Rows[i]["ID"].ToString());
this.BindData();
i++;
}
}
private void UpdatePatient(string id)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("UPDATE tblNurs SET Is_Active = '1' WHERE ID = @Id", con))
{
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
public static int i = 0;
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblNurs", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
Records after all row checked
ID |
No |
Name |
Is_Active |
1 |
123 |
ABC |
1 |
2 |
234 |
SKY |
1 |
3 |
456 |
XYZ |
1 |
4 |
567 |
PQR |
1 |