Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to fetch records from my SQL DB and writing them to a text file when user clicks on the button. what I want is, no. of records is to be shown to the user during the process. like if there are 10000 records (say), so when user clicks on button, there should be a progress showing "No. of records written: 1,2,3.... " and so on. means user must be able to see that how many records have been actually written. here is my code, plz help to edit it to get the desired thing.

XML
<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true">
    </asp:ScriptManager>
    <asp:Timer runat="server" ID="Timer1" Interval="1000" Enabled="false" OnTick="Timer1_Tick" />
    <asp:UpdatePanel ID="UpdatePanell" runat="server" UpdateMode="Always">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:TextBox ID="Label1" runat="server" TextMode="MultiLine" Height="250px" width="800px"/>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="Button2" runat="server" Text="Insert Records2"
        onclick="Button2_Click" />
    <asp:UpdateProgress ID="UpdateProgress1" AssociatedUpdatePanelID="UpdatePanell" runat="server">
        <ProgressTemplate>
            Update in Progress……..
        </ProgressTemplate>
    </asp:UpdateProgress>
</div>
</form>


and here is code behind:

protected void Button2_Click(object sender, EventArgs e)
{
string filename = @"C:\Insert\Myfile.txt";
string cs = "Data Source=machinename;Initial Catalog=TestDB;Integrated Security=SSPI;Persist Security Info=True;";
string sql = "SELECT ID from Employee";

if (File.Exists(filename))
File.Delete(filename);
using (StreamWriter writer = new StreamWriter(filename))
{
SqlConnection conn = new SqlConnection(cs);
conn.Open();
SqlCommand command = new SqlCommand(sql, conn);
var reader = command.ExecuteReader();
while (reader.Read())
{
StringBuilder myData = new StringBuilder();
myData.Append(reader["ID"].ToString());
writer.WriteLine(myData.ToString());
}
conn.Dispose();
conn.Close();
writer.Close();
}
}
Posted
Updated 6-Aug-15 20:32pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900