简体   繁体   中英

Error - No value given for one or more required parameters

I have a problem working on my project.

I'm trying to read a data from an Excel file. It works fine when I'm trying to select rows which are greater than Col1Value but after I add AND Gender = " + gender; it gives me error "NO VALUE GIVEN FOR ONE OR MORE REQUIRED PARAMETERS" I cannot set a specific gender column because It is different on every excel file although column name is same and error appears when I'm trying to fill the DataSet.

if (boxGender.Text != "")

string gender = boxGender.Text;
string col1Name = lbl1stColumn.Text;


string Query = "select * from [data$] where " + 
               col1Name + " > " + Col1Value + 
               " AND Gender = " + gender;                                                
OleDbDataAdapter dacol1 = new OleDbDataAdapter(Query, con);                        
    Column1Data.Clear();
    dacol1.Fill(Column1Data)
    lblStuCount1Col.Text = Column1Data.Tables[0].Rows.Count.ToString();

You need to enclose the string value in single quotes and the column names in square brackets:

string Query = "select * from [data$] where [" + 
               col1Name + "] > " + Col1Value + 
               " AND Gender = '" + gender + "'"; 

I think you might be missing quotes in your SQL query:

string Query = "select * from [data$] where " + col1Name + " > '" + Col1Value + "' AND Gender = '" + gender +"'";

Note single quote (') symbols added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM