Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have to read a csv file and upload the data into a datatable but the issue is I have column called amount in which the user gives data like 10,31,218.00
So when am reading the data from csv file its considering that particular amount data as 3 different columns as its csv.
can anyone help me in restricting csv file where amount data is comma seperated.
Thanks in advance
Posted
Comments
E.F. Nijboer 14-Aug-12 6:53am    
The restriction is given in the file extension 'csv', meaning 'comma separated values'. There is no hard restriction so you would need to check that yourself.
AmitGajjar 14-Aug-12 6:56am    
is CSV is in your hand ? can you modify CSV data generator ?

You could change the delimiter in CSV file or, add some sort of escape character in the file. Your C# code, could never be sure if the comma is delimiter or number separator.
 
Share this answer
 
Comments
qwerty 2 14-Aug-12 6:59am    
how can i know whether comma is delimiter or number separator.
i mean i just want to restrict the file from uploading when the amount is comma seperated
dan!sh 14-Aug-12 7:35am    
You or the code in this context cannot. And hence you need to change the structure of the file.
CSV is a rather useless outdated concept to be honest. It would be better to use DSV (delimiter separated values) instead. You can just take any separator you like (and agree on) and escape special characters using a backslash.

If you still are going to use csv you can agree on the number decimal being a point and numbers separated using a comma. Another solution is to simply surround each value with quotes.

Good luck!
 
Share this answer
 
The way I approach this scenario little differently. I would write my own text processor to read non standard csvs I recieve. It's very simple and you can always read any such stuffs using "Regular Expressions"

1. Read the file using C# by line by line.
2. Split the records into columns using RegEx

I will explain with small sample working code:

C#
Regex _Expression;
private StreamReader textReader;
private string CSVSourceFeedPath;

textReader = new StreamReader(CSVSourceFeedPath);
string _Statement = String.Format
        ("{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))",
                        Regex.Escape(";"), Regex.Escape("\"")); //define your delimeters i used semicolon ; here

RegexOptions _Options = RegexOptions.Compiled | RegexOptions.Multiline;
_Expression = new Regex(_Statement, _Options);

string nextLine;
string[] columns;



nextLine = textReader.ReadLine();
while (nextLine != null)
{
    columns = _Expression.Split(nextLine);
    if (columns.Length >= 20//your expected number of columns)
    {
//process here. columns[0] will be your first column
    }
nextLine = textReader.ReadLine();
}
 
Share this answer
 
v3
for reading excel/csv refer following link
Read Excel in ASP.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900