I am uploading a file through a file upload control and then stream reader reads the file itno a datatable and then sql bulk copy copies the datatable to my sql database and fills the appropriate columns. Does anyone see anything wrong with the code below? I dont get an error message but it seems like it is getting hung up on the IIS process. I cant go in to delete the csv file from the folder because it says the process is still working.
protected void btnUpload_Click(object sender, EventArgs e)
{
//upload file to the gencouploadfiles folder
UploadFile();
//fetch CSV file from the folder
string strFilePath = Server.MapPath("GencoUploadFiles") + "\\" + "GencoUploadFile.txt";
//perform sql bulk copy
PerformBulkCopy(GencoUpload(strFilePath));
//delete the file from the folder
}
public void UploadFile()
{
if (fileUpload1.HasFile)
{
FileInfo fileinfo = new FileInfo(fileUpload1.PostedFile.FileName);
string strCsvFilePath = Server.MapPath("GencoUploadFiles") + "\\" + "GencoUploadFile.txt";
fileUpload1.SaveAs(strCsvFilePath);
}
}
public static DataTable GencoUpload(string filepath)
{
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string[] value = line.Split('|');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split('|');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
return dt;
}
public void PerformBulkCopy(DataTable dt)
{
SqlConnection conStr = new SqlConnection(ConfigurationManager.ConnectionStrings["EDI"].ConnectionString);
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conStr.ConnectionString))
{
bulkcopy.DestinationTableName = "dbo.GencoUploadTempTable";
bulkcopy.BatchSize = dt.Rows.Count;
conStr.Open();
bulkcopy.WriteToServer(dt);
bulkcopy.Close();
conStr.Close();
}
}