csv file reading only first line - c#

I'm trying to upload a series of client info's through a csv ,I had some trouble with this in the eginning but my previous post was answered so I was able to start reading in the data however it only reads the first line. Just wondering if anyone had any ideas. I've included the code below
private void btnUpload_Click(object sender, EventArgs e)
{
//Browse for file
OpenFileDialog ofd = new OpenFileDialog();
//Only show .csv files
ofd.Filter = "Microsoft Office Excel Comma Separated Values File|*.csv";
DialogResult result = ofd.ShowDialog();
//If the user selects a valid file
if (result == DialogResult.OK)
{
//File is delimited by a comma
char[] laClientDelim = { ',' };
//New object for string manipulation
objStringManipulation = new StringManipulation();
// Parse the csv file
List<string[]> lsClientList = objStringManipulation.parseCSV(ofd.FileName, laClientDelim);
foreach (string[] laClient in lsClientList)
{
//Create new object for manipulating the database
objSqlCommands = new SqlCommands("Client", "ClientName");
string[] records = File.ReadAllLines(ofd.FileName); // read the file completely line by line
char splitChar = ',';
int splitCharCount = 0;
int k = 0;
string[] fields = records[k].Split(splitChar); // reads all the single values per line. 'splitChar' should be the delimiter.
splitCharCount++;
if (splitCharCount >= 4 && splitCharCount <= 10)
{
var stuff = from l in File.ReadLines(ofd.FileName)
let x = l.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Select(s => char.Parse(s))
select new
{
Client = x,
ClientName = x
};
}
//Inserts the client info into datbase
objSqlCommands.sqlCommandInsertorUpdate("INSERT", records[k]);//laClient[0]);
k++;
//Refreshs the Client table on display from the
this.clientTableAdapter.Fill(this.kIIDImplementationCalcDataSet.Client);
//MAKE SURE TO ONLY ADD IN CLIENT AND CLIENT NAME
//update the view
dgvClientlst.Update() ;
}
}
}

your loop looks like this essentially :
foreach (string[] laClient in lsClientList)
{
int k = 0;
string[] records = File.ReadAllLines(ofd.FileName);
string[] fields = records[k].Split(splitChar);
k++;
}
Your 'k' value never makes it past 0 for each laClient. You need to loop internally for each line.

I know my answer is not exactly what you are looking for but if you convert the .csv file to .xls file then you can manipulate it very easily. I have done this on multiple occasions and if you want I can provide you instructions with code.

Expanding on Jonesy's answer (because I can't yet comment), declare the variable k outside the loop. It's being reset to zero each time through.
int k = 0;
foreach (string[] laClient in lsClientList)
{
string[] records = File.ReadAllLines(ofd.FileName);
string[] fields = records[k].Split(splitChar);
k++;
}

Related

How to read numbers in a .txt file to an integer array?

I have a file that I need to save as an array. I am trying to convert a text file to an integer array using StreamReader. I just am unsure as to what to put in the for loop at the end of the code.
This is what I have so far:
//Global Variables
int[] Original;
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//code to load the numbers from a file
OpenFileDialog fd = new OpenFileDialog();
//open the file dialog and check if a file was selected
if (fd.ShowDialog() == DialogResult.OK)
{
//open file to read
StreamReader sr = new StreamReader(fd.OpenFile());
int Records = int.Parse(sr.ReadLine());
//Assign Array Sizes
Original = new int[Records];
int[] OriginalArray;
for (int i = 0; i < Records; i++)
{
//add code here
}
}
The .txt file is:
5
6
7
9
10
2
PS I am a beginner, so my coding skills are very basic!
UPDATE: I have previous experience using Line.Split and then mapping file to arrays but obviously that does not apply here, so what do I do now?
//as continued for above code
for (int i = 0; i < Records; i++)
{
int Line = int.Parse(sr.ReadLine());
OriginalArray = int.Parse(Line.Split(';')); //get error here
Original[i] = OriginalArray[0];
}
You should just be able to use similar code to what you had above it:
OriginalArray[i] = Convert.ToInt32(sr.ReadLine());
Every time the sr.ReadLine is called it increments the data pointer by 1 line, hence iterating through the array in the text file.
try this
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(fd.OpenFile());
var list = new List<int>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
int value = 0;
if (!string.IsNullOrWhiteSpace(line) && int.TryParse(line, out value))
list.Add(value);
}
MessageBox.Show(list.Aggregate("", (x, y) => (string.IsNullOrWhiteSpace(x) ? "" : x + ", ") + y.ToString()));
}
You can read the entire file into a string array, then parse (checking the integrity of each one).
Something like:
int[] Original;
//Load File
private void mnuLoad_Click_1(object sender, EventArgs e)
{
//code to load the numbers from a file
var fd = new OpenFileDialog();
//open the file dialog and check if a file was selected
if (fd.ShowDialog() == DialogResult.OK)
{
var file = fd.FileName;
try
{
var ints = new List<int>();
var data = File.ReadAllLines(file);
foreach (var datum in data)
{
int value;
if (Int32.TryParse(datum, out value))
{
ints.Add(value);
}
}
Original = ints.ToArray();
}
catch (IOException)
{
// blah, error
}
}
}
Another way to do it if you'd like to read to the end of the file and you don't know how long it is with a while loop:
String line = String.Empty;
int i=0;
while((line = sr.ReadLine()) != null)
{
yourArray[i] = Convert.ToInt32(line);
i++;
//but if you only want to write to the file w/o any other operation
//you could just write w/o conversion or storing into an array
sw.WriteLine(line);
//or sw.Write(line + " "); if you'd like to have it in one row
}
//using linq & anonymous methods (via lambda)
string[] records = File.ReadAllLines(file);
int[] unsorted = Array.ConvertAll<string, int>(records, new Converter<string, int>(i => int.Parse(i)));

How to incorporate a text qualifier in a file name used as a variable?

I have a C# script which takes in two CSV files as input, combines the two files, performs numerous calculations on them, and writes the result in a new CSV file.
These two input CSV file names are declared as variables and are used in the C# script by accessing those variable names.
The data in the input CSV files looks like this:
Since the data has values in thousands and millions, line splits in the C# code are truncating the data incorrectly. For instance a value of 11,861 appears only as 11 and 681 goes in the next columns.
Is there any way in C#, by which I can specify a text qualifier (" in this case) for the two files ?
Here is the C# code snippet:
string[,] filesToProcess = new string[2, 2] { {(String)Dts.Variables["csvFileNameUSD"].Value,"USD" }, {(String)Dts.Variables["csvFileNameCAD"].Value,"CAD" } };
string headline = "CustType,CategoryType,CategoryValue,DataType,Stock QTY,Stock Value,Floor QTY,Floor Value,Order Count,Currency";
string outPutFile = Dts.Variables["outputFile"].Value.ToString();
//Declare Output files to write to
FileStream sw = new System.IO.FileStream(outPutFile, System.IO.FileMode.Create);
StreamWriter w = new StreamWriter(sw);
w.WriteLine(headline);
//Loop Through the files one by one and write to output Files
for (int x = 0; x < filesToProcess.GetLength(1); x++)
{
if (System.IO.File.Exists(filesToProcess[x, 0]))
{
string categoryType = "";
string custType = "";
string dataType = "";
string categoryValue = "";
//Read the input file in memory and close after done
StreamReader sr = new StreamReader(filesToProcess[x, 0]);
string fileText = sr.ReadToEnd();
string[] lines = fileText.Split(Convert.ToString(System.Environment.NewLine).ToCharArray());
sr.Close();
where csvFileNameUSD and csvFileNameCAD are variables with values pointing to their locations.
Well, based on the questions you have answered, this ought to do what you want to do:
public void SomeMethodInYourCodeSnippet()
{
string[] lines;
using (StreamReader sr = new StreamReader(filesToProcess[x, 0]))
{
//Read the input file in memory and close after done
string fileText = sr.ReadToEnd();
lines = fileText.Split(Convert.ToString(System.Environment.NewLine).ToCharArray());
sr.Close(); // redundant due to using, but just to be safe...
}
foreach (var line in lines)
{
string[] columnValues = GetColumnValuesFromLine(line);
// Do whatever with your column values here...
}
}
private string[] GetColumnValuesFromLine(string line)
{
// Split on ","
var values = line.Split(new string [] {"\",\""}, StringSplitOptions.None);
if (values.Count() > 0)
{
// Trim leading double quote from first value
var firstValue = values[0];
if (firstValue.Length > 0)
values[0] = firstValue.Substring(1);
// Trim the trailing double quote from the last value
var lastValue = values[values.Length - 1];
if (lastValue.Length > 0)
values[values.Length - 1] = lastValue.Substring(0, lastValue.Length - 1);
}
return values;
}
Give that a try and let me know how it works!
You posted a very similar looking question few days ago. Did that solution not help you?
If so, what issues are you facing on that. We can probably help you troubleshoot that as well.

Problems reading multiple lines from a text file and saving them into an array c#

So what I want to be able to do is read a file that has one data segment that reads like this. So far the program opens the file from a drop down menu but I am having a hard time saving them into array. I wish to be able to click a next button after opening the file (Where it prints the last three lines from the text file into the text boxes) on a form application, and it prints each information line in the text file example below into a separate text box. This is where I am having the problem.
The names and addresses are to be saved to a EmpNames Class, and then the .split() numbers below are to be saved into their own respective Employee Class so as to be set into a series of calculations, then print the result in a text box.
1
John MerryWeather
123 West Main Street
5.00 30
There will be multiple data segments like this, but no more than 10. This is what I have so far.
public partial class Form1 : Form
{
const int MAX = 10;
public Form1()
{
InitializeComponent();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = #"C:\";
if (theDialog.ShowDialog() == DialogResult.OK)
{
//Declarations:
// linesPerEmployee: Controls the number of lines to be read.
// currEmployeeLine: Controls where in the file you are reading.
Employee employee = new Employee();
NameAdd empNames = new NameAdd();
string filename = theDialog.FileName;
List<Employee> employeeList = new List<Employee>();
int linesPerEmployee = 4;
int currEmployeeLine = 0;
//parse line by line into instance of employee class
while (employeeList != null)
{
string[] filelines = File.ReadAllLines(filename);
if (filelines != null)
{
employee.EmpNum = int.Parse(filelines[0]);
empNames.Name =
}
}
Instead of reading all lines in one chunk you could read them line by line and add each line into a List<string> for example, to easier handle the "lines"
var employees = new List<string>();
Stream file = theDialog.File.OpenRead();
while((line = file.ReadLine()) != null)
{
employees.Add(line);
}
And then loop through the employee list to parse each 4 lines into an Employee()
Still, I agree with the comments about using a better format instead.
too agree with the others about a better file format, but also, what happens if your data gets out of whack, missing or extra lines, any confirmation about sequence numbering between employees, bad data that can't be converted... all this and more say bad idea to current format.
However, that being said, and what you already have going, I would wrap it up something like...
string[] filelines = File.ReadAllLines(filename);
if (filelines != null)
{
if (filelines.Length % 4 == 0)
{
// which array element are we getting to at the start of each employee.
int arrayBase = 0;
for( int i=0; i < (int)(filelines.Length / 4); i++ )
{
arrayBase = i*4;
employee.EmpNum = int.Parse(filelines[arrayBase]);
empNames.Name = filelines[arrayBase + 1];
empNames.Address = filelines[arrayBase + 2];
string[] rateAndHours = filelines[arrayBase + 3].Split(' ');
// you would still have to parse the rate and hours though.
double justRate = double.Parse(rateAndHours[0]);
int justHours = int.Parse(rateAndHours[1]);
// obviously add your own try\catch confirmation on parsing issues
// and ultimately store in your record entries
}
}
}

C# Adding 4923 Lines from a text file to DataGridView by skipping blank lines

I am new to C#. I have a huge text file consisting of 4923 lines of data that I wish to add to a DataGridView. The text file has a lot of spaces and blank lines in between sentences. I want that all the blank lines and spaces be skipped and load the contents to the DataGridView. Can someone give me a solution to achieve this? Can this be achieved without using DataTables and Datasource?
If my question is not clear please let me know. There may be mistakes in my code. Please help me rectify it
Thanks
This is my code
public void textload()
{
List<string> str = new List<string>();
String st = "";
//Path to write contents to text file
string filename = #"E:\Vivek\contentcopy\clientlist.txt";
Form.CheckForIllegalCrossThreadCalls = false;
OpenFileDialog ofd = new OpenFileDialog();
ofd.FileName = "";
ofd.Filter = "csv files|*.csv|txt files|*.txt|All Files|*.*";
ofd.ShowDialog();
st = ofd.FileName;
if (string.IsNullOrEmpty(ofd.FileName))
return;
string[] lines = File.ReadAllLines(st);
for (int i = 0; i < lines.Length; i++)
{
listBox1.Items.Add(lines[i]);
string[] s = lines[i].Split(' ');
MessageBox.Show(s.Length.ToString());
str.Add(lines[i]);
dataGridView1.Rows.Add();
if (s[i] == null && s[i] == " ")
{
continue;
}
dataGridView1.Rows[i].Cells[i].Value=s[i];
//dataGridView1.Rows[i].Cells[10].Value = s[10];
//dataGridView1.Rows[i].Cells[11].Value = s[11];
//dataGridView1.Rows[i].Cells[12].Value = s[12];
//dataGridView1.Rows[i].Cells[13].Value = s[13];
//dataGridView1.Rows[i].Cells[14].Value = s[14];
//dataGridView1.Rows[i].Cells[15].Value = s[15];
}
File.WriteAllLines(filename, str);
dataGridView1.ReadOnly = true;
}
One option is to filter out empty strings as soon as you've read it. This can be done with LINQ Where and passing condition that checks string for not being whitespace only. String.IsNullOrWhitepspace covers that (in addition it would check for null that will not happen in case of using ReadAllLines).
string[] lines = File.ReadAllLines(st)
.Where(s => !String.IsNullOrWhitespace(s))
.ToArray();

Eliminate special characters from CSV with C#

I am getting data from a CSV file through my Web Api with this code
private List<Item> items = new List<Item>();
public ItemRepository()
{
string filename = HttpRuntime.AppDomainAppPath + "App_Data\\items.csv";
var lines = File.ReadAllLines(filename).Skip(1).ToList();
for (int i = 0; i < lines.Count; i++)
{
var line = lines[i];
var columns = line.Split('$');
//get rid of newline characters in the middle of data lines
while (columns.Length < 9)
{
i += 1;
line = line.Replace("\n", " ") + lines[i];
columns = line.Split('$');
}
//Remove Starting and Trailing open quotes from fields
columns = columns.Select(c => { if (string.IsNullOrEmpty(c) == false) { return c.Substring(1, c.Length - 2); } return string.Empty; }).ToArray();
var temp = columns[5].Split('|', '>');
items.Add(new Item()
{
Id = int.Parse(columns[0]),
Name = temp[0],
Description = columns[2],
Photo = columns[7]
});
}
}
But the CSV file returned data with special characters instead of an apostrophe.
For example in the CSV file the are values such as There’s which should be "There's" or "John’s" which should be "John's".
This ’ is there instead of an apostrophe.
How do I get rid of this to just show my apostrophe.
This kind of data is being returned in
Name = temp[0],
Description = columns[2],
You can use the HttpUtility.HtmlDecode to convert the characters. Here's an example:
var withEncodedChars = "For example in the CSV file the are values such as There’s which should be There's or John’s which should be John's. This ’ is there instead of an apostrophe.";
Console.WriteLine(HttpUtility.HtmlDecode(withEncodedChars));
If you run this in a console app it outputs:
For example in the CSV file the are values such as There's which should be There's or John's which should be John's. This ' is there instead of an apostrophe.

Categories

Resources