0

I have a plain text file that contains one column of numbers, and in a web client take the numbers and transfer them into a SQL Database. After retrieving the file, I store the contents in a temporary DataTable, then using SqlBulkCopy, attempt to transfer the DataTable to the database. However, once I run the web client and import the file, the program hangs. The code is listed below for what I've done so far.

Transferring Files to DataTable

private DataTable readTextFile()
    {
        DataTable dt = new DataTable();
        FileUpload file = new FileUpload();

        dt.Columns.Add("Claim Number", typeof(Int32)); 
        file = (FileUpload)grdCriteria.FindControl("exportUpload");
        StreamReader read = new StreamReader(file.PostedFile.FileName);

        while ((read.ReadLine()) != null)
            dt.Rows.Add((Int32.Parse(read.ReadLine()))); 

        return dt; 
    }

Insert DataTable into SQL Database

DataTable dt = readTextFile();
SqlBulkCopy bk = new SqlBulkCopy(Profile.ConnectionKey.CAM);

bk.DestinationTableName = dt.TableName;
bk.WriteToServer(dt);

Thanks!

Delfino
  • 967
  • 4
  • 21
  • 46
  • Do you mean to call ReadLine twice in the loop? Or do you mean to do something like: while(true) { string line = read.ReadLine(); if(line != null) { dt.Rows.Add(int.Parse(line)); } else { break; } – Robert Horvick Jul 30 '13 at 19:33
  • Where does the program hang? Did you try to debug the program? How large is the file? Could it be that the file is just too large and it takes a lot of time to read it or push it to the server? – Panagiotis Kanavos Jul 30 '13 at 19:34
  • You are not setting the TableName value, so the database loader will not know what table to put the data in. Also, you're only reading in every second value, because you are calling ReadLine() twice in every loop iteration. – Jeffrey L Whitledge Jul 30 '13 at 19:48

0 Answers0