Click here to Skip to main content
15,881,826 members
Please Sign up or sign in to vote.
2.53/5 (3 votes)
See more:
Hi,
 
I am working on uploading the csv file and saving the data into database. I have done with saving part but what i need is to validate the csv with correct data farmat. 
-----------------------------------------------
Name  Address    Age  Gender
---------------------------------------------
a     Hyderabad  23    M
b     Banglore   25    F
c     Mumbai     26    M
---------------------------------------------
 
The Data In csv file need  to be In above format while upload. If they enter data in below format and trying to upload it then an error message need to be display saying "Upload Valid CSV File".
-----------------------------------------------
Name  Address   (Column Name Is missing)  Gender| Column0
--------------------------------------------
a     Hyderabad            23                M   ( Some Junk Data)
b     Banglore             25                F       | 
c     Mumbai               26                M       |
---------------------------------------------
 
--------------------------------------------
Xyz        olp
--------------------------------------------

 
I have googled a lot but did not found valid links which suits for my problem.
Posted
Updated 28-Aug-14 1:22am
v2

1 solution

1.If column names are fixed means you already know column names compare the column names with data base column names.
To do this first fill the data table from excel then you compare with column names.

C#
ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", @"C:\file.xlsx"); 

objConn.Open(); 
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM ["xlWorksheet"$]", objConn); 

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); 

objAdapter1.SelectCommand = objCmdSelect; 

DataSet objDataset1 = new DataSet(); 

objAdapter1.Fill(objDataset1); 

objConn.Close();


C#
// For each DataTable, print the ColumnName.
foreach(DataTable table in objDataset1 .Tables)
{
    foreach(DataColumn column in table.Columns)
    {
        Console.WriteLine(column.ColumnName);// gives the all column names in excel.
//compare here columns
    }
}
 
Share this answer
 
v2
Comments
santoshkumar413 28-Aug-14 7:13am    
@vangapally Naveen Kumar, Thanks for the reply. I have done in the same way which you have mentioned, but the loading time is higher for the above process. Is there any other way to validate the sheet?
vangapally Naveen Kumar 28-Aug-14 7:30am    
try like this

using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();

DataTable sheetColumns = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, sheetName, null }));
foreach (DataColumn sheetColumnProperties in sheetColumns.Rows)
{
// From here you have access to the column's name, type...
}
}

santoshkumar413 28-Aug-14 7:54am    
Thanks for the reply i will implement in the same way.
vangapally Naveen Kumar 28-Aug-14 8:09am    
welcome..............

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