Sir good morning
I have made a function where I am exporting data into excel and it is working fine but after successful export data alert message in not displaying.
please help me.
CS
protected void BtnExportReport_Click(object sender, EventArgs e)
{
if (TxtStockDate.Text == "")
{
MessageTitle = "Select Date";
MessageSwalTitle = "Oops !!!";
MessageSwalDisplay = MessageTitle;
MessageSwalType = "warning"; // info , success , warning , error
ScriptManager.RegisterStartupScript(this, this.GetType(), "Script", "FunctionAlertMessage('" + MessageSwalTitle + "', '" + MessageSwalDisplay + "', '" + MessageSwalType + "');", true);
return;
}
//if (RbtnListReport.SelectedIndex < 0)
//{
// MessageTitle = "Report Option Not Selected";
// MessageSwalTitle = "Oops !!!";
// MessageSwalDisplay = MessageTitle;
// MessageSwalType = "warning"; // info , success , warning , error
// ScriptManager.RegisterStartupScript(this, this.GetType(), "Script", "FunctionAlertMessage('" + MessageSwalTitle + "', '" + MessageSwalDisplay + "', '" + MessageSwalType + "');", true);
// return;
//}
//if (RbtnListReport.SelectedIndex == 0)
//{
// ReportOption = "BLOCKED";
// ExportReport();
// //ExportReportBlockedList();
//}
//else if (RbtnListReport.SelectedIndex == 1)
//{
// ReportOption = "UNBLOCKED";
// //ExportReportBlockedList();
// ExportReport();
//}
BtnExportReport.Enabled = false;
ExportReport();
BtnExportReport.Enabled = true;
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "swal('Success!','Report Downloaded Successfully' , 'success')", true);
return;
}
// End Btn Export
protected void ExportReport()
{
try
{
// SET DATABASE CONNECTION.
ConnectionStringFunction();
SqlCn = new SqlConnection(ConPath);
SqlCn.Open();
List<SqlParameter> sqlParameterExport = new List<SqlParameter>();
DateTime DateTimeStart = DateTime.ParseExact(TxtStockDate.Text, "MM/dd/yyyy", null); // textbox show be read only =false
DateTimeStart = Convert.ToDateTime(DateTimeStart, System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
//"hi-IN" is culture information about India. You can change as per culture like French, German, etc.
//DateTime DateTimeEnd = DateTime.ParseExact(TxtEndDate.Text, "MM/dd/yyyy", null); // textbox show be read only =false
//DateTimeEnd = Convert.ToDateTime(DateTimeEnd, System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
////"hi-IN" is culture information about India. You can change as per culture like French, German, etc.
FromDate = DateTimeStart.ToString("MM/dd/yyyy");
//ToDate = DateTimeEnd.ToString("MM/dd/yyyy");
sqlParameterExport.Add(new SqlParameter("MainCompanyCode", int.Parse(TxtUserCompanyCode.Text)));
sqlParameterExport.Add(new SqlParameter("@Date", FromDate));
SqlDt = new DataTable();
SqlCm = new SqlCommand("ReportVehicleStock", SqlCn);
SqlCm.CommandType = CommandType.StoredProcedure;
if (sqlParameterExport.ToArray().Length > 0)
{
SqlCm.Parameters.AddRange(sqlParameterExport.ToArray());
}
SqlDa = new SqlDataAdapter(SqlCm);
SqlDa.Fill(SqlDt);
//// CALCULATE RUNNING TOTAL (WILL DISPLAY AT THE FOOTER OF EXCEL WORKBOOK.)
//Decimal dTotalPrice = 0;
//for (int i = 0; i <= SqlDt.Rows.Count - 1; i++)
//{
// //dTotalPrice += SqlDt.Rows[i].Field<Decimal>(1); // 1 is cell no
//}
// NOW ASSIGN DATA TO A DATAGRID.
DataGrid dg = new DataGrid();
dg.DataSource = SqlDt;
dg.DataBind();
// THE EXCEL FILE.
string sFileName = "VehicleStock_" + System.DateTime.Now + ".xls";
sFileName = sFileName.Replace("/", "");
// SEND OUTPUT TO THE CLIENT MACHINE USING "RESPONSE OBJECT".
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + sFileName);
Response.ContentType = "application/vnd.ms-excel";
EnableViewState = false;
////======================
//StringBuilder sb = new StringBuilder();
//sb.AppendLine("AAAAAA");
//sb.AppendLine("BBBBBB");
//Response.Write(sb.ToString());
////======================
System.IO.StringWriter objSW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter objHTW = new System.Web.UI.HtmlTextWriter(objSW);
dg.GridLines = GridLines.Both;
dg.HeaderStyle.Font.Bold = true; // SET EXCEL HEADERS AS BOLD.
dg.HeaderStyle.BackColor = System.Drawing.Color.LightBlue;
dg.RenderControl(objHTW);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
string ReportTitle = "<table><tr><td> <style> font-size:medium; color:blue </style> <b>" + "Report Name : " + "Vehicle Stock as on " + DateTimeStart.ToString("dd/MMM/yyyy") + "</b></td> </tr></table>";
string ReportDate = "<table><tr><td> <style> font-size:medium; color:blue </style> <b>" + "Report Date : " + System.DateTime.Now.ToLongDateString() + "</b></td> </tr></table>";
string ReportTime = "<table><tr><td> <style> font-size:medium; color:blue </style> <b>" + "Report Time : " + System.DateTime.Now.ToShortTimeString() + "</b></td> </tr></table>";
// ADD A ROW AT THE END OF THE SHEET SHOWING A RUNNING TOTAL OF PRICE.
Response.Write(ReportTitle);
Response.Write(ReportDate);
Response.Write(ReportTime);
// STYLE THE SHEET AND WRITE DATA TO IT.
Response.Write("<style> TABLE { border:solid 1px #999; } " +
"TD { border:solid 1px #D5D5D5; text-align:left; } </style>");
//Response.Write("<style> TABLE { border:dotted 1px #999; } " +
// "TD { border:dotted 1px #D5D5D5; text-align:left; } </style>");
Response.Write(objSW.ToString());
//// ADD A ROW AT THE END OF THE SHEET SHOWING A RUNNING TOTAL OF PRICE.
//Response.Write("<table><tr><td><b>Total: </b></td><td></td><td><b>" +
// dTotalPrice.ToString("N2") + "</b></td></tr></table>");
Response.End();
dg = null;
MessageTitle = "Report Successfuly Downloaded";
TransactionStatus = true;
}
catch (Exception ex)
{
//MessageTitle = "Transaction Successful \\n\\Job Card No : " + TmpDocumentNo.Trim() + " Generated";
TransactionStatus = false;
//SqlTran.Rollback();
MessageTitle = "Transaction Failed !!!!" + "<br/>" + ex.Message.ToString();
}
finally
{
SqlCn.Close();
}
//End Finally
// ========================= End Try
}