Israel

Israel

  • 693
  • 1.3k
  • 219.4k

Make filter Between number

Feb 27 2017 2:21 AM
Hi!
 
With two comboboxes I need to make filter from one database column.
 
I wrote these codes to facilitate me to make filter between a couple of numbers. For instance, display number between 3 to 8.
I do try to write these codes but its doesnt works. Please have a look:
 
private void picBxAgeCons_Click(object sender, EventArgs e)
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;

cmd.CommandText = "select * from myDataBase where (Age between ? and ?)";

var param1 = new OleDbParameter("@StartAge", OleDbType.Numeric);
var param2 = new OleDbParameter("@EndAge", OleDbType.Numeric);

param1.Value = cbxAgeStart.Text;
param2.Value = cbxAgeEnd.Text;

if (cbxAgeStart.Text <= cbxAgeEnd.Text)
{
cmd.Parameters.Add(param1);
cmd.Parameters.Add(param2);
}
else
{
cmd.Parameters.Add(param2);
cmd.Parameters.Add(param1);
}

cmd.ExecuteNonQuery();

DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);

conn.Close();
dgvIdadeCons.DataSource = dt;

this.dgvIdadeCons.RowsDefaultCellStyle.BackColor = Color.AliceBlue;
this.dgvIdadeCons.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige; 

Answers (6)