Reputation: 31
I want to run 2 functions which is displayTable() and ExportCSV() on RunKDF_OnClick button, but i only able to run one of the function which is ExportCSV(). Below is my code.
public void RunKDF_OnClick(object sender, EventArgs e)
{
ConnectionInfo connectionInfo = new ConnectionInfo("xx.xx.xx.xxx", xx, "root", new
AuthenticationMethod[]
{
// Pasword based Authentication
new PasswordAuthenticationMethod("root","xxxx"),
});
using (SshClient ssh = new SshClient(connectionInfo))
{
ssh.Connect();
ssh.RunCommand("perl -w /tmp/ThermalValue/bin/kdf2csv/thermalKDF.pl");
ssh.RunCommand("exit");
ssh.Disconnect();
}
displayTable();
ExportCSV();
}
public void displayTable()
{
string connectionString = @"Data Source = xxxxx; port = xxxx; Initial Catalog = xxxx; User Id = xxxxx; password = xxxxx";
using (MySqlConnection sqlCon = new MySqlConnection(connectionString))
{
sqlCon.Open();
MySqlDataAdapter sqlDa = new MySqlDataAdapter("SELECT UID AS UNIT_ID ,Tvalue AS Temperature_Value , File AS KDF_File_Name FROM OEE_PROD.thermal", sqlCon);
DataTable table = new DataTable();
sqlDa.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
}
//This is ExportCSV method.
public void ExportCSV() { string conn = @"Data Source = xxxxx; port = xxxx; Initial Catalog = OEE_PROD; User Id = xxxx; password = xxxxx"; using (MySqlConnection con = new MySqlConnection(conn)) {
using (MySqlCommand cmd = new MySqlCommand("SELECT UID AS UNIT_ID, Tvalue AS Temperature_Value, File AS KDF_File_Name FROM OEE_PROD.thermal"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
da.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Build the CSV file data as a Comma separated string.
string csv = string.Empty;
foreach (DataColumn column in dt.Columns)
{
//Add the Header row for CSV file.
csv += column.ColumnName + ',';
}
//Add new line.
csv += "\r\n";
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
}
//Add new line.
csv += "\r\n";
}
//Download the CSV file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=KDFExport_"+DateTime.Now+".csv");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(csv);
Response.Flush();
Response.End();
}
}
}
}
//This is my aspx for gridview
<asp:GridView CssClass="GridView" Width="100%" ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="No">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerSettings FirstPageText="First" LastPageText="Last" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Upvotes: 0
Views: 136
Reputation: 1167
The problem is that you are calling Response.End
to early.
https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.end?view=netframework-4.8
ends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event
Upvotes: 1