Command text has not been set for the command object

I am working on a project and getting an error because "the command text has not been set for the command object." My code is:

query = "select Top 10    
                 Name,
                 R_Line2,
                 BirthDate,
                 BirthTime,
                 Height,
                 Weight,
                 BirthCity,
                 BirthCountry,
                 FatherName,
                 MonthlyIncome,
                 FamilyIncome,
                 Add1,
                 Add2,
                 PinCode,
                 Tel1,
                 Tel2
          from InpRegistration  
          where DateDiff('yyyy', [Birthdate],
                  Now()) BETWEEN @AgeFrom AND
                  @AgeTo and Weight BETWEEN @MinWeight AND 
                  @MaxWeight and Height BETWEEN @MinHeight AND @MaxHeight and   
                  MonthlyIncome BETWEEN @MinIncome AND @MaxIncome";

connf.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data   Source=D:\project\Milan_Data.mdb";

connf.Open();

   OleDbCommand cmdf = new OleDbCommand(query, connf);

    cmdf.CommandType = CommandType.Text;

        cmdf.Parameters.AddWithValue("@AgeFrom", ddlminage.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@AgeTo", ddlmaxage.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@MinWeight", ddlweightmin.SelectedValue);
        cmdf.Parameters.AddWithValue("@MaxWeight", ddlweightmax.SelectedValue);
        cmdf.Parameters.AddWithValue("@MinHeight", ddlheightmin.SelectedValue);
        cmdf.Parameters.AddWithValue("@MaxHeight", ddlheightmax.SelectedValue);
        cmdf.Parameters.AddWithValue("@MinIncome", ddlminincome.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@MinIncome", ddlmaxincome.SelectedItem.Text);

        OleDbDataAdapter daf = new OleDbDataAdapter(cmdf);
        DataSet dsf = new DataSet();
        daf.Fill(dsf);
        Repeater2.DataSource = dsf;
        Repeater2.DataBind();


        connf.Close();

Please help me. I am searching the net for this, but not getting any solution. A similar problem . Thanks in advance...

+4
source share
1 answer

How he helped you, I change the comment as an answer

The query was first declared as a string, so change it to sqlcommand as shown below.

SqlCommand query = new sqlcommand(); 
query.commandText = "select Top 10..."     

Final version:

SqlCommand query = new sqlcommand();   
query.commandText = "select Top 10    
                 Name,
                 R_Line2,
                 BirthDate,
                 BirthTime,
                 Height,
                 Weight,
                 BirthCity,
                 BirthCountry,
                 FatherName,
                 MonthlyIncome,
                 FamilyIncome,
                 Add1,
                 Add2,
                 PinCode,
                 Tel1,
                 Tel2
          from InpRegistration  
          where DateDiff('yyyy', [Birthdate],
                  Now()) BETWEEN @AgeFrom AND
                  @AgeTo and Weight BETWEEN @MinWeight AND 
                  @MaxWeight and Height BETWEEN @MinHeight AND @MaxHeight and   
                  MonthlyIncome BETWEEN @MinIncome AND @MaxIncome";

connf.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data   Source=D:\project\Milan_Data.mdb";

connf.Open();

   OleDbCommand cmdf = new OleDbCommand(query, connf);

    cmdf.CommandType = CommandType.Text;

        cmdf.Parameters.AddWithValue("@AgeFrom", ddlminage.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@AgeTo", ddlmaxage.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@MinWeight", ddlweightmin.SelectedValue);
        cmdf.Parameters.AddWithValue("@MaxWeight", ddlweightmax.SelectedValue);
        cmdf.Parameters.AddWithValue("@MinHeight", ddlheightmin.SelectedValue);
        cmdf.Parameters.AddWithValue("@MaxHeight", ddlheightmax.SelectedValue);
        cmdf.Parameters.AddWithValue("@MinIncome", ddlminincome.SelectedItem.Text);
        cmdf.Parameters.AddWithValue("@MinIncome", ddlmaxincome.SelectedItem.Text);

        OleDbDataAdapter daf = new OleDbDataAdapter(cmdf);
        DataSet dsf = new DataSet();
        daf.Fill(dsf);
        Repeater2.DataSource = dsf;
        Repeater2.DataBind();


        connf.Close();
+5
source

All Articles