Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how do i read Fields with embedded commas must be delimited with double-quote characters.

1997,Ford,E350,"Super, luxurious truck"

plz help me with few codes to get it into table from csv file text (ASP.net)

output

year car modelno modelname

1997 Ford E350 Super luxurious truck
Posted
Comments
Prasad Avunoori 17-Jul-14 7:01am    
Does it contain 4 fields exactly?
10923679 17-Jul-14 7:05am    
in this row i have
year car modelno modelname
1997, Ford,E350, "Super, luxurious truck"
yes it has 4 fields
10923679 17-Jul-14 7:09am    
try
{

FileUpload1.PostedFile.SaveAs(filename);

string[] Lines = File.ReadAllLines(filename);
string[] Fields;

Lines = Lines.Skip(1).ToArray();
List<employeemaster> emlist = new List<employeemaster>();

foreach (var line in Lines)
{
Fields = line.Split(new char[] { ',' });
emlist.Add(
new EmployeeMaster
{
EmployeeID = Fields[0].Replace("\"", ""),
CompanyName = Fields[1].Replace("\"", ""),
ContactName = Fields[2].Replace("\"", ""),
ContactTitle = Fields[3].Replace("\"", ""),
CompanyAddress = Fields[4].Replace("\"", ""),
PostalCode_ = Fields[5].Replace("\"", ""),

});
}

using (MuDatabaseEntities dc = new MuDatabaseEntities())
{
foreach (var i in emlist)
{
var v = dc.EmployeeMasters.Where(a => a.EmployeeID.Equals(i.EmployeeID)).FirstOrDefault();
if (v != null)
{
v.EmployeeID = i.EmployeeID;
v.CompanyName = i.CompanyName;
v.ContactName = i.ContactName;
v.ContactTitle = i.ContactTitle;
v.CompanyAddress = i.CompanyAddress;
v.PostalCode_ = i.PostalCode_;
}
else
{
dc.EmployeeMasters.Add(i);
}
}

dc.SaveChanges();
this is also another example with 6 fields but when in csv file
i get
company address-"54,abc,HBC"
then 54 abc HBC treated as different field.
i need it within " " field must be treated as single one in ASP
Prasad Avunoori 17-Jul-14 8:05am    
Find my solution below. Adapt it to your requirement.

C#
static void EmbeddedCommas()
        {
            string input =@"1997,Ford,E350,""Super, luxurious truck""";
            string[] inputArray;
            inputArray = input.Split(',');
            int count = 0;
            int maxCount = 4;
            string[] outputArray = new string[maxCount];

            foreach (var item in inputArray)
            {
                if (count < maxCount - 1)
                {
                    outputArray[count] = item;
                    input = input.Remove(0, outputArray[count].Length + 1);
                    count++;
                }
                else
                {
                    outputArray[count] = input.Replace('"',' ');
                }

            }

            foreach (var item in outputArray)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
 
Share this answer
 
csv: Comma Separated Values
Look for a library which can read csv files!
 
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