I have encounter an error which is
The given value of type String from the data source cannot be converted to type nvarchar of the specified target column.
on below code. This code I been use since last month and its working, I'm not sure whats the root cause of this. Kindly advise
DataTable sheetTable = loadSingleSheet(@"" + var_SourceFilePath + "MBF_Cancel_Temp.xlsx" + "", sheetName);
loading sheet as below
private static DataTable loadSingleSheet(string fileName, string sheetName)
{
DataTable sheetData = new DataTable();
using (OleDbConnection conn = returnConnection(fileName))
{
conn.Open();
// retrieve the data using data adapter
OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "] ", conn);
sheetAdapter.Fill(sheetData);
//OleDbCommand oledbcmd = new OleDbCommand("select F1, F2, F3, F4, F7, F8, F9, F10, F11, F12, F13, F14, F15, F5 from [" + sheetName + "] WHERE F5 = '100,000'", conn);
OleDbCommand oledbcmd = new OleDbCommand("select F1, F2, F3, F4, F5, F6, F7, F8, F9, F10 from [" + sheetName + "] ", conn);
//OleDbCommand oledbcmd = new OleDbCommand("select NO, LOC, [EFF DATE], [EXP DATE], [NAME], [I#C], [D#O#B], [ADD1], [ADD2], [ADD3], [ADD4], [ADD5], [P#C], [BEN#NAME], [BEN#I#C] from [" + sheetName + "] ", conn);
//conn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(var_MSDBConn);
bulkcopy.DestinationTableName = "AMB_Cancel_Temp";
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
conn.Close();
}
return sheetData;
}
private static DataTable loadSingleSheet(string fileName, string sheetName)
{
DataTable sheetData = new DataTable();
using (OleDbConnection conn = returnConnection(fileName))
{
conn.Open();
// retrieve the data using data adapter
OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "] ", conn);
sheetAdapter.Fill(sheetData);
//OleDbCommand oledbcmd = new OleDbCommand("select F1, F2, F3, F4, F7, F8, F9, F10, F11, F12, F13, F14, F15, F5 from [" + sheetName + "] WHERE F5 = '100,000'", conn);
OleDbCommand oledbcmd = new OleDbCommand("select F1, F2, F3, F4, F5, F6, F7, F8, F9, F10 from [" + sheetName + "] ", conn);
//OleDbCommand oledbcmd = new OleDbCommand("select NO, LOC, [EFF DATE], [EXP DATE], [NAME], [I#C], [D#O#B], [ADD1], [ADD2], [ADD3], [ADD4], [ADD5], [P#C], [BEN#NAME], [BEN#I#C] from [" + sheetName + "] ", conn);
//conn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(var_MSDBConn);
bulkcopy.DestinationTableName = "AMB_Cancel_Temp";
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
conn.Close();
}
return sheetData;
}