Search code examples
c#datasetoledbdataadapter

C# - OleDBDataAdapter.Fill(DataSet) throws "No value given for one or more required parameters. "


I have the following code to access a .xlsx file and get the sheet named "Sheet1" as a table. Then I select the values for URL and Username using select command.

string strPath = @"C:\Users\...\TestData.xlsx";
string strExcelConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";");
OleDbConnection connExcel = new OleDbConnection(strExcelConn);
try
{
    connExcel.Open();
    DataTable dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();

    OleDbCommand cmdExcel = new OleDbCommand("SELECT URL, Username From [" + SheetName + "]",connExcel);

    DataSet ds = new DataSet();

    OleDbDataAdapter da = new OleDbDataAdapter("SELECT URL, Username From [" + SheetName + "]", strExcelConn);
    cmdExcel.CommandText = "SELECT URL, Username From [" + SheetName + "]";
    Console.WriteLine(cmdExcel.CommandText);

    da.Fill(ds);
    connExcel.Close();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
    connExcel.Close();
}

Unfortunately, I'm getting the following error during the datafill of the adapter.

da.Fill(ds);

The error says

Message: System.Data.OleDb.OleDbException : No value given for one or more required parameters.

Could someone help me find a solution for this?


Solution

  • The problem was only when I tried to select specific columns. If I have Select * from, it works fine.

    But, not sure about the actual issue though.