OLEDB Data Access – Error

Recently I land up in a simple error in the below code. The application just showed "OleDbException: No value given for one or more required parameters." Any guess what is the issue here? I do have Excel file at the right place. I have all the three columns.

using System;
using System.Data;
using System.Data.OleDb;

namespace ExcelData
{
class
Program
{
static
void Main(string[] args)
{
OleDbConnection excelconn = new
OleDbConnection();


//string exclConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\abc.xls;Extended Properties=’Excel 12.0;HDR=YES’";

string exclConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\abc.xls;Extended Properties=’Excel 8.0;HDR=YES’";

excelconn.ConnectionString = exclConnectionString;
excelconn.Open();


OleDbCommand cmd = new
OleDbCommand(@"SELECT CustomerGroupCode, CustomerGroupDesc, CustomerGroupParent FROM [CustomerGroupHierarchy$]", excelconn);
OleDbDataAdapter cmdadapter = new
OleDbDataAdapter(cmd);


DataTable workingTable = new
DataTable();

cmdadapter.Fill(workingTable);
cmdadapter = null;
}
}
}

 

Ok… The issue is in Excel one of the column name has one extra space at the beginning. If you have space at end of the column name, the code works without any issue. But if the column has prefixed with space, OLEDB shows irrelevant error message "No value given for one or more required parameters".

Leave a comment