Click here to Skip to main content
15,888,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When i try to get data from excel sheet i get error

no value given for one or more required parameters.'

why this happen .

i work in visual studio 2010 with excelsheet 2013 

my code in c# as following

	<pre lang="c#">public DataTable ShowWrongExcelData()

	       {

	           string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtPath.Text);

	 

	           OleDbConnection con = new OleDbConnection(connectionString);

	 

	 

	           con.Open();

	           string str = @"SELECT  [رقم الاستمارة] as [UnitCode],[قراءة العداد]as[CurrentMeterReading] FROM  [Sheet5$] where [CurrentMeterReading] <= 0 ";

	         

	           OleDbCommand com = new OleDbCommand();

	           com = new OleDbCommand(str, con);

	           OleDbDataAdapter oledbda = new OleDbDataAdapter();

	           oledbda = new OleDbDataAdapter(com);

	           DataSet ds = new DataSet();

	           ds = new DataSet();

	           oledbda.Fill(ds, "[Sheet5$]");

	           con.Close();

	           System.Data.DataTable dt = new System.Data.DataTable();

	           dt = ds.Tables["[Sheet5$]"];

	           return dt;

	 

	 

	       }


why give this error i don't use any parameters .

but when i select all data from Excel sheet as following :

@"SELECT [رقم الاستمارة] as [UnitCode],[قراءة العداد]as[CurrentMeterReading] FROM [Sheet5$]";

I get data without any problem

so that How to solve this error please ?

Excel sheet InvoiceData.xlsx sample

[^]

What I have tried:

error no value given for one or more required parameters.' select from excelsheet 
Posted
Updated 20-Apr-18 23:55pm

1 solution

If I understand your question correctly you don't have a column named CurrentMeterReading in your Excel sheet, that's just an alias that you give for a column when selecting the data. The column name in WHERE clause should refer the column names in the sheet.

Have a try with something like
C#
string str = @"SELECT  [رقم الاستمارة] as [UnitCode],[قراءة العداد]as[CurrentMeterReading] 
FROM  [Sheet5$] 
WHERE [قراءة العداد] <= 0 ";
 
Share this answer
 

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