Click here to Skip to main content
15,893,381 members
Home / Discussions / C#
   

C#

 
GeneralRe: Binding in-memory data to a DataGridView Pin
Henry Minute2-Apr-09 15:04
Henry Minute2-Apr-09 15:04 
GeneralRe: Binding in-memory data to a DataGridView Pin
StevenS_Dev3-Apr-09 13:22
StevenS_Dev3-Apr-09 13:22 
Questioncalling method/event of one form from another Pin
Sandeep Kalra2-Apr-09 8:22
Sandeep Kalra2-Apr-09 8:22 
AnswerRe: calling method/event of one form from another Pin
Colin Angus Mackay2-Apr-09 9:17
Colin Angus Mackay2-Apr-09 9:17 
GeneralRe: calling method/event of one form from another Pin
Sandeep Kalra2-Apr-09 18:09
Sandeep Kalra2-Apr-09 18:09 
Questionnetworkable sign in sign out board C# Pin
Rafone2-Apr-09 6:25
Rafone2-Apr-09 6:25 
AnswerRe: networkable sign in sign out board C# Pin
Skymir2-Apr-09 9:47
Skymir2-Apr-09 9:47 
QuestionCSV File Splitter Help... Pin
Alistair Jarrett2-Apr-09 5:36
Alistair Jarrett2-Apr-09 5:36 
Please bare with me I only started learning c# a few days back.
Anyway I am creating a split function (by line not file size) and thus far I have got it to create do the necessary math & create its directory. However I have a problem in my loop in which the error: Cannot write to a closed TextWriter. I just cant seem to solve this problem and need some help!

 <br />
private void btnSplit_Click(object sender, EventArgs e)<br />
        {<br />
            string inputFile = this.txtFileToImport.Text; // Substitute this with your Input File <br />
            FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);<br />
            int numberOfFiles = (int)this.numericUpDown1.Value;<br />
            int numberOfRows = (int)this.dataGridView_preView.RowCount - 1;<br />
            decimal rowsPerFile = numberOfRows / numberOfFiles;<br />
            int sizeOfEachFile = (int)Math.Ceiling((double)rowsPerFile);<br />
            lblPray.Text = " Rows: " + sizeOfEachFile.ToString();<br />
<br />
            string OutputFolder = inputFile + "_Batches";<br />
            if (Directory.Exists(inputFile) == false)<br />
<br />
            {<br />
                Directory.CreateDirectory(OutputFolder);<br />
            }<br />
   <br />
            // Read the csv column's header<br />
            System.IO.StreamReader reader = new System.IO.StreamReader(inputFile);<br />
            string strHeader = reader.ReadLine();<br />
<br />
            // Start splitting<br />
            int FileIndex = 0;<br />
<br />
            do<br />
                {<br />
<br />
                // Create new file to store a piece of the csv file<br />
                string PiecePath = OutputFolder + "\\" + Path.GetFileNameWithoutExtension(inputFile) + "_" + FileIndex + Path.GetExtension(inputFile);<br />
                StreamWriter Writer = new StreamWriter(PiecePath, false);<br />
                Writer.AutoFlush = false;<br />
                Writer.WriteLine(strHeader);<br />
<br />
                // Read and writes precise number of rows<br />
                for (int i = 1; i <= sizeOfEachFile; i++)<br />
                {<br />
<br />
                    string s = reader.ReadLine();<br />
                    if (s != null & _IsAbort == false)<br />
                    {<br />
                        Writer.WriteLine(s);<br />
                    }<br />
                    else<br />
                    {<br />
                        Writer.Flush();<br />
                        Writer.Close();<br />
                    }<br />
                }<br />
<br />
                // Flush and close the splitted file<br />
                Writer.Flush();<br />
                Writer.Close();<br />
            }<br />
            while (true);<br />
        }       <br />
    }<br />
}<br />


Ive probably been looking straight at the problem and just been sitting here far to long to realise... either that or its all wrong! Any help would be much appreciated.

Cheers Thumbs Up | :thumbsup:
AnswerRe: CSV File Splitter Help... Pin
Judah Gabriel Himango2-Apr-09 5:52
sponsorJudah Gabriel Himango2-Apr-09 5:52 
AnswerRe: CSV File Splitter Help... Pin
Le centriste2-Apr-09 5:52
Le centriste2-Apr-09 5:52 
AnswerRe: CSV File Splitter Help... Pin
DaveyM692-Apr-09 6:02
professionalDaveyM692-Apr-09 6:02 
AnswerRe: CSV File Splitter Help... Pin
riced2-Apr-09 7:05
riced2-Apr-09 7:05 
GeneralRe: CSV File Splitter Help... Pin
Alistair Jarrett2-Apr-09 22:46
Alistair Jarrett2-Apr-09 22:46 
GeneralRe: CSV File Splitter Help... Pin
riced3-Apr-09 3:59
riced3-Apr-09 3:59 
QuestionAn easy one... problems converting a decimal to an integer Pin
bbranded2-Apr-09 5:31
bbranded2-Apr-09 5:31 
AnswerRe: An easy one... problems converting a decimal to an integer Pin
DaveyM692-Apr-09 5:37
professionalDaveyM692-Apr-09 5:37 
GeneralRe: An easy one... problems converting a decimal to an integer Pin
bbranded2-Apr-09 5:44
bbranded2-Apr-09 5:44 
GeneralRe: An easy one... problems converting a decimal to an integer Pin
DaveyM692-Apr-09 5:50
professionalDaveyM692-Apr-09 5:50 
AnswerRe: An easy one... problems converting a decimal to an integer Pin
harold aptroot2-Apr-09 6:08
harold aptroot2-Apr-09 6:08 
GeneralRe: An easy one... problems converting a decimal to an integer Pin
bbranded2-Apr-09 7:06
bbranded2-Apr-09 7:06 
GeneralRe: An easy one... problems converting a decimal to an integer Pin
harold aptroot2-Apr-09 8:19
harold aptroot2-Apr-09 8:19 
GeneralRe: An easy one... problems converting a decimal to an integer Pin
Luc Pattyn2-Apr-09 8:53
sitebuilderLuc Pattyn2-Apr-09 8:53 
GeneralRe: An easy one... problems converting a decimal to an integer Pin
harold aptroot2-Apr-09 9:11
harold aptroot2-Apr-09 9:11 
AnswerRe: An easy one... problems converting a decimal to an integer Pin
Luc Pattyn2-Apr-09 8:58
sitebuilderLuc Pattyn2-Apr-09 8:58 
Question1st Post and Code snip so far Pin
stonebergftw2-Apr-09 5:03
stonebergftw2-Apr-09 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.