Eimantas Baigys
Eimantas Baigys

Reputation: 147

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();

Upvotes: 0

Views: 7167

Answers (2)

Kamil Krasinski
Kamil Krasinski

Reputation: 529

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.

Upvotes: 1

D Stanley
D Stanley

Reputation: 152644

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 + "'"; 

Upvotes: 1

Related Questions