Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
2
Answers
csv to datatable sudden error
Daniel Koh
7y
245
1
Reply
string
CSVFilePathName = @
"C:\test.csv"
;
string
[] Lines = File.ReadAllLines(CSVFilePathName);
string
[] Fields;
Fields = Lines[0].Split(
new
char
[] {
','
});
int
Cols = Fields.GetLength(0);
DataTable dt =
new
DataTable();
//1st row must be column names; force lower case to ensure matching later on.
for
(
int
i = 0; i < Cols; i++)
dt.Columns.Add(Fields[i].ToLower(),
typeof
(
string
));
DataRow Row;
for
(
int
i = 1; i < Lines.GetLength(0); i++)
{
Fields = Lines[i].Split(
new
char
[] {
','
});
Row = dt.NewRow();
for
(
int
f = 0; f < Cols; f++)
Row[f] = Fields[f];
dt.Rows.Add(Row);
}
This is the code I have, which was working fine yesterday. However today without any changes made to the code it says:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Any possible solutions?
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
Generate digital signature in PDF and verify the certificate
Tablelayout panel control