1
I was wondering if you could help me solve a problem that I’m having some difficulty solving. The problem is this:
I have a database with a table of values in which I store:
- date: datetime
- value: int
- equipment: int (Foreign key coming from equipment table)
What I want to do is a search in which I want the user to fill in two textboxes that receive a date in each of them and then I want to click a search button that appears a gridview with all the values of this table but only those that are in the range of dates chosen by the user in the two textboxes. I am working with ASP.NET with C# and my database is Mysql
protected void botaoPequisar_Click(object sender, EventArgs e)
{
try
{ var conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["basedados"].ConnectionString);
DateTime data1 = DateTime.ParseExact(txtDataInicio.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
DateTime data2 = DateTime.ParseExact(txtDataFim.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
MySqlDataAdapter da = new MySqlDataAdapter("SELECT idConduta,valor_Lido,data_Leitura FROM valores_conduta WHERE data_Leitura BETWEEN " + data1.ToString("yyyy-MM-dd") + " AND " + data2.ToString("yyyy-MM-dd") + " AND idConduta=" + ddlHistorico.SelectedValue + "", conn);
da.SelectCommand.CommandType = CommandType.Text;
DataSet ds = new DataSet();//definir o objecto dadaset (ds)
//preencher os dados
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch(Exception ex) {
}
}
Mario, pass the filters via
Parameter
!– Vinicius Carvalho
Attention, the Datetime goes from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
– Tony
Datetime.Tostring("yyyy-MM-dd HH:mm:ss")
– Tony