I have an excel sheet with 18 columns. In that 5 columns have same column name CALL_REASON. But when i load that excel sheet into a dataset using Microsoft.ACE.OLEDB.12.0. The column names in dataset are changed for the columns which had same name. it is getting loaded as CALL_REASON,CALL_REASON1,CALL_REASON2,CALL_REASON3,CALL_REASON4. They are present in excelsheet as CALL_REASON,CALL_REASON,CALL_REASON,CALL_REASON,CALL_REASON
String properties = "Excel 8.0; HDR=YES; IMEX=1;";//properties set for connection to excel
string sSourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\uploads\" + fileName + ";Extended Properties=\"" + properties + "\"";
sSourceConnection = new OleDbConnection(sSourceConstr);//creating the OLEDB connection
try
{
//select statement to select data from the first excel sheet
string sql = string.Format("Select * FROM [{0}]", "Sheet1$");
//commands to fill the dataset with excel data
OleDbDataAdapter excelAdapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand(sql, sSourceConnection);
sSourceConnection.Open();
excelAdapter.SelectCommand = command;
excelAdapter.Fill(surveyItemDetails, "ExcelDataTable");
}
You cannot have fields with the same name in a DataTable. In fact, you cannot even have
CALL_REASONandCALL_reason, see, for example, theExceptionssection in the documentation of DataColumnCollection.Add:The requirement that column names must be unique makes perfect sense. What should be returned by
DataRow.Field<String>("CALL_REASON"), if you were allowed to have multiple columns with that name?