I have controller that do a try catch when uploading like this :
try
{
connExcel.Open();
cmdExcel.CommandText = "SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
// dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
//here the Sweetalert or just alert
}
I want to make a sweetalert2 popup that said "the template is not match" when not meet the try condition.
What should I do? I have installed swal library in _layout too, because I used it in JS Ajax before, but still confused with this case.
The whole action:
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
var path = Server.MapPath("~/Uploads/");
var filePath = string.Empty;
var extension = string.Empty;
var dtSheet = new DataTable();
var ExcelData = new DataSet();
var user = User.Identity.Name;
var prefix = user + DateTime.Now.ToString("yyyyMMddHHmmss");
if (postedFile != null)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
filePath = path + Path.GetFileName(postedFile.FileName.Replace(postedFile.FileName,prefix));
extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath+extension);
}
if (postedFile == null)
return new EmptyResult();
var connectionString = string.Empty;
//milih constring tergantung file
switch (extension)
{
case ".xls": //<= 2003 version
connectionString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //>= 2007 version
connectionString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
//oledb koneksi
connectionString = string.Format(connectionString, filePath+extension);
using (var connExcel = new OleDbConnection(connectionString))
{
using (var cmdExcel = new OleDbCommand())
{
using (var odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
//ambil nama sheet #1
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
var sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
try
{
connExcel.Open();
cmdExcel.CommandText =
"SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
//dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
//here the Sweetalert or just alert
}
}
}
}
ExcelData.Tables.Add(dtSheet);
return View(ExcelData);
}
Thanks :)
You can not return the sweetalert from controller. You have to set it up to view side. Here is example
try
{
connExcel.Open();
cmdExcel.CommandText = "SELECT NAMA , REPLACE(NOHP, '-', '' ) as NOHP,TANGGAL, NOMINAL From [" + sheetName +
"] WHERE NAMA IS NOT NULL OR NoHP IS NOT NULL OR Tanggal IS NOT NULL OR NOMINAL IS NOT NULL";
odaExcel.SelectCommand = cmdExcel;
// dapetin data dimasukin ke dtSheet
odaExcel.Fill(dtSheet);
connExcel.Close();
}
catch (Exception)
{
ViewBag.Message = "Your Message";
}
On html page in script tag.
<script>
$(function() {
var message = '@ViewBag.Message';
if (message != '')
swal({ title: "Done", text: message , icon: "error" });
});
</script>