Hi indradeo,
Use Ajax Timer control.
Check this example. Now please take its reference and correct your code.
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="OnSaveFile" Interval="10000" />
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Code
C#
protected void OnSaveFile(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Customers";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
if (!Directory.Exists(Server.MapPath("~/Files")))
{
Directory.CreateDirectory(Server.MapPath("~/Files"));
}
StringBuilder sb = new StringBuilder();
for (int row = 0; row < dt.Rows.Count; row++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
sb.Append(dt.Rows[row][column].ToString());
sb.Append(column == dt.Columns.Count - 1 ? "\n" : ",");
}
sb.AppendLine();
}
StreamWriter sw = new StreamWriter(Path.Combine(Server.MapPath("~/Files"), "Sample.txt"), false);
sw.WriteLine(sb.ToString());
sw.Close();
}
VB.Net
Protected Sub OnSaveFile(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM Customers"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
If Not Directory.Exists(Server.MapPath("~/Files")) Then
Directory.CreateDirectory(Server.MapPath("~/Files"))
End If
Dim sb As StringBuilder = New StringBuilder()
For row As Integer = 0 To dt.Rows.Count - 1
For column As Integer = 0 To dt.Columns.Count - 1
sb.Append(dt.Rows(row)(column).ToString())
sb.Append(If(column = dt.Columns.Count - 1, vbLf, ","))
Next
sb.AppendLine()
Next
Dim sw As StreamWriter = New StreamWriter(Path.Combine(Server.MapPath("~/Files"), "Sample.txt"), False)
sw.WriteLine(sb.ToString())
sw.Close()
End Sub