Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hii ,

I am using csv helper to retrive my data from csv file ,

but before that i want to add into database , i just want to check wether i have all columns properly or not into that csv file , so i want to check number of coulmns count

how can i do that

C#
 using (System.IO.StreamReader file = new System.IO.StreamReader(Server.MapPath(filepath)))
                        {
                            //Csv reader reads the stream
                            CsvReader csvread = new CsvReader(file);
                            var recs = csvread.GetRecords<dynamic>().ToArray();
                            int length = recs.Length;
                            while (csvread.Read())
                            {   
                                var intField = csvread.GetField<string>("HeaderName");
                                var stringField = csvread.GetField<string>(1);
                            }
}
Posted
Updated 13-Aug-15 20:57pm
v3
Comments
Torakami 13-Aug-15 9:12am    
My above code is not giving me proper count of columns availble into uploaded csv file

1 solution

XML
using (System.IO.StreamReader file = new System.IO.StreamReader(Server.MapPath(filepath)))
                        {
                            //Csv reader reads the stream
                            CsvReader csvread = new CsvReader(file);
                            while (csvread.Read())
                            {
                                int count = csvread.FieldHeaders.Count();
                                if (count == 55)
                                {
                                    DataRow dr = myExcelTable.NewRow();
                                    if (csvread.GetField<string>("FirstName") != null)
                                    {
                                        dr["FirstName"] = csvread.GetField<string>("FirstName"); ;
                                    }
                                    else
                                    {
                                        dr["FirstName"] = "";
                                    }

                                    if (csvread.GetField<string>("LastName") != null)
                                    {
                                        dr["LastName"] = csvread.GetField<string>("LastName"); ;
                                    }
                                    else
                                    {
                                        dr["LastName"] = "";
                                    }
                                }
                                else
                                {
                                    lblMessage.Visible = true;
                                    lblMessage.Text = "Columns are not in specified format.";
                                    lblMessage.ForeColor = System.Drawing.Color.Red;
                                    return;
                                }
                            }
                            }
 
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