I'm using asp.net MVC 4 to make a website where user can upload .xlsx
file and save the data to MSSQL table. I want to make sure that there is no illegal characters such as SQL injection statements in the file before saving the datas. So far I tested out with $
sign it works fine but it'll only catch if a cell has only that character, not in between characters. Here is my code,
Controller
public ActionResult BulkReadings()
{
string pathToExcelFile = System.IO.Path.Combine(Server.MapPath("~/ExcelFiles/"), "BulkReads.xlsx");
string sheetName = "Sheet1";
var excelFile = new ExcelQueryFactory(pathToExcelFile);
var getSheet = from a in excelFile.Worksheet(sheetName) select a;
string Subject = "";
string Type = "";
string Reading = "";
foreach (var a in getSheet)
{
if (a["Subject"] == "$" || a["Type"] == "$" || a["Reading"] == "$") // This is where it checks for the "$" sign
{
if (System.IO.File.Exists(pathToExcelFile))
{
System.IO.File.Delete(pathToExcelFile);
}
TempData["meter_fail"] = "Error! Illegal Characters!";
return RedirectToAction("MeterManager");
}
else
{
Subject = a["Subject"];
Type = a["Type"];
Reading = a["Reading"];
try
{
Reading newEntry = new Reading();
newEntry.title = Subject;
newEntry.type = Type;
newEntry.reading1 = Reading;
rentdb.Readings.Add(newEntry);
}
catch
{
if (System.IO.File.Exists(pathToExcelFile))
{
System.IO.File.Delete(pathToExcelFile);
}
TempData["meter_fail"] = "Error! Upload Failed!";
return RedirectToAction("MeterManager");
}
}
}
rentdb.SaveChanges();
if (System.IO.File.Exists(pathToExcelFile))
{
System.IO.File.Delete(pathToExcelFile);
}
TempData["meter_success"] = "Reading(s) uploaded successfully!";
return RedirectToAction("MeterManager");
}
How can I check for multiple illegal characters that can be present as single or with other characters in the cell?