Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command text was not set for the command object

I am working on a project and getting an error as "Command text was not 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 on net for this but not getting any solution.Similar Problem,. Thanks in advance...

like image 945
Santosh Kumar Avatar asked Mar 21 '23 01:03

Santosh Kumar


1 Answers

As it helped you, i am changing the comment as an answer

Query was first declared as string so change it to sqlcommand as given 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();
like image 157
Amarnath Balasubramanian Avatar answered Apr 09 '23 15:04

Amarnath Balasubramanian