Command text was not set for the command object
asp.net, c#, ms-access-2007, runtime-error, sql-server
Solution
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();
Problem
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...