Search code examples
c#oledb

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

Solution

  • 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.