Convert a date into yyyy-dd-mm format to dd-mm-yyyy

Asked

Viewed 3,095 times

1

I’m trying to do a search in my database where the goal is to search between two dates entered by the user, and that it shows me all the values that are in this date range, but I’m not able to get the date on texbox because it gives format error. I want to present 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();
  • 1

    What is the format error?

  • Is this what you want? http://answall.com/questions/11065/converter-data-dd-mm-aaaaaa-para-formato-8601-aaaaaamm-dd

1 answer

1

You can use the .ToString("dd-MM-yyyy") on your date (must be a valid date), leaving:

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 variable dt in your consultation.

  • There is a bug because the date starts at dd-mm-yyyy 00:00:00 until dd-mm-yyyy 23:59:59

  • Then in the .ToString("dd-MM-yyyy") just do .ToString("dd-MM-yyyy HH:mm:ss")

  • txtDataInicio.Text is String, not Datetime. Error: The best overloaded method match for 'string.Tostring(System.Iformatprovider)' has some invalid Arguments

  • If it is string do the example below. Convert to Datetime and then convert to the String format you want

  • Okay thanks I’ll try to do that already I give you a feedback!!

  • I did so and gave myself a syntax error again, but with debugg I could see that it is going to fetch the format I want, but when it does fill the dataset with the dataadapter it complains if Datetime dt1 = Datetime.Parseexact(txtDataInicio.Text, "yyyy-M-d", Cultureinfo.Invariantculture); dt1.Tostring("d-M-yyyy");

  • Yeah, that’s a different question, and I’m not seeing a solution...

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.