Convert a date in the format yyyy-dd-mm to dd-mm-yyyy [duplicate]

1

I am trying to do a search in my database where the purpose is to search between two dates entered by the user, and that it shows me all the values that are in that date range, but I am not able to go get the date in texbox because it gives format error. I want to display the search result in a GridView

MySqlDataAdapter da = new MySqlDataAdapter("SELECT idConduta,valor_Lido FROM valores_conduta WHERE data_Leitura BETWEEN " + txtDataInicio.Text + " AND " + txtDataInicio.Text + "", 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();
    
asked by anonymous 14.11.2014 / 15:50

1 answer

1

You can use .ToString("dd-MM-yyyy") on your date (it has to be a valid date), getting:

MySqlDataAdapter da = new MySqlDataAdapter("SELECT idConduta,valor_Lido FROM valores_conduta WHERE data_Leitura BETWEEN " + txtDataInicio.Text.ToString("dd-MM-yyyy") + " AND " + txtDataInicio.Text.ToString("dd-MM-yyyy")** + "", conn);

Or you can convert the date before your query:

DateTime dt = DateTime.ParseExact(txtDataInicio.Text, "yyyy-dd-mm", 
                                  CultureInfo.InvariantCulture);
dt.ToString("dd-MM-yyyy");

And you use the dt variable in your query.

    
14.11.2014 / 15:53