Highlighted are the method signatures. This function however, cannot work in the context of an <asp:UpdatePanel because of the usage of the HttpResponse object. If however, lots of web controls or given that the page itself is encapsulated in the update panel, we have to set the buttons to behave synchronously.
<asp:UpdatePanel ID="updatePanel_CSV" runat="server">
<Triggers>
<%--Response object must be synchronous--%>
<asp:PostBackTrigger ControlID="but_importCSV" />
<asp:PostBackTrigger ControlID="but_exportToCSV" />
</Triggers>
<ContentTemplate>
<div runat="server" id="div_upload"...
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
public class CXUtil_CSVParser
{
public void ExportToCSV(string CSV, string fileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment; filename=" + fileName + ".csv");
HttpContext.Current.Response.AddHeader("Pragma", "public");
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.Write(CSV);
HttpContext.Current.Response.End();
}
public string ConvertDataTableToCSV(DataTable table)
{
string result = "";
for (int x = 0; x < table.Columns.Count; x++)
{
result += x == 0 ? "" : ",";
result += GetDataTableCell(table.Columns[x].ColumnName);
}
result += Environment.NewLine;
foreach (DataRow row in table.Rows)
{
for (int x = 0; x < table.Columns.Count; x++)
{
result += x == 0 ? "" : ",";
result += GetDataTableCell(row[x].ToString());
}
result += Environment.NewLine;
}
return result;
}
public DataTable ConvertCSVtoDataTable(Stream stream)
{
DataTable table = new DataTable();
try
{
StreamReader reader = new StreamReader(stream);
List<string> header = GetCsvRow(reader);
foreach (string column in header)
{
table.Columns.Add(column);
}
while (!reader.EndOfStream)
{
List<string> cells = GetCsvRow(reader);
if (cells.Count > 0)
{
DataRow row = table.NewRow();
bool isRowEmpty = true;
for (int x = 0; x < cells.Count && x < header.Count; x++)
{
row[header[x]] = cells
;
if (!string.IsNullOrEmpty(cells[x]))
{
isRowEmpty = false;
}
}
if (!isRowEmpty)
{
table.Rows.Add(row);
}
}
}
}
catch (Exception ex)
{
throw new Exception("Cannot parse IO stream: " + ex.Message);
}
return table;
}
public bool IsValidString(String strToCheck)
{
bool charIsValid = true;
string[] reserved_words = {
"select", ">",
"select * from",
"drop table",
"insert into",
"exec", "insert", "insert into", "update", "delete", "drop"};
for (int x = 0; x < reserved_words.Length; x++)
{
if (strToCheck.IndexOf(reserved_words[x]) != -1)
{
charIsValid = false;
}
}
return charIsValid;
}
public bool IsValidDate(string dateString)
{
bool result = true;
try
{
Convert.ToDateTime(dateString);
}
catch (FormatException fe)
{
result = false;
}
return result;
}
#region .. private methods ..
private List<string> GetCsvRow(StreamReader reader)
{
List<string> row = new List<string>();
bool inQuote = false;
string cell = "";
int i = reader.Read();
while (i != -1)
{
char c = Convert.ToChar(i);
if (!inQuote && c == ',')
{
row.Add(cell.Trim().Trim('\"'));
cell = "";
}
else if (!inQuote && c == Environment.NewLine[0])
{
string newLineCheck = "" + c;
bool isNewLine = true;
for (int j = 1; j < Environment.NewLine.Length; j++)
{
int newLineCharInt = reader.Read();
if (newLineCharInt == -1)
{
isNewLine = false;
break;
}
char newLineChar = Convert.ToChar(newLineCharInt);
newLineCheck += newLineChar;
if (newLineChar != Environment.NewLine[j])
{
isNewLine = false;
break;
}
}
if (isNewLine)
{
row.Add(cell.Trim().Trim('\"'));
break;
}
cell += newLineCheck;
}
else
{
if (c == '\"')
{
inQuote = !inQuote;
}
cell += c;
}
i = reader.Read();
}
return row;
}
private string GetDataTableCell(string content)
{
string result = content.Trim();
if (result.IndexOf(',') > -1 ||
result.IndexOf(Environment.NewLine) > -1)
{
result = '\"' + result + '\"';
}
return result;
}
#endregion
}