Character string not recognized as valid datetime

Asked

Viewed 9,820 times

2

protected void ButtonPesquisar_Click(object sender, EventArgs e)
{
    var _macro = new LFSistemas.VLI.ACTWeb.Entities.Macro();

    var macroController = new MacroController();
    var itens = macroController.ObterTodos(new Entities.FiltroMacro()
    {
    //DataInicio = Convert.ToDateTime(TextBoxDataInicio.Text + "" + TextBoxHoraInicio.Text),
    //DataFim = Convert.ToDateTime(TextBoxHoraFim.Text + "" + TextBoxHoraFim.Text),
    DataInicio = new DateTime(2013, 08, 12, 20, 10, 00),
    //DateTime.Now.AddSeconds(10)
        DataFim = new DateTime(2013, 08, 12, 20, 30, 00)
    });

Today my code is running this way, but I want to search my data through the information informed in the textbox.

<div class="row">
    <div class="form-group col-lg-2 col-md-5 col-sm-5 col-xs-12">
        <label for="data_inicio">Data Inicio</label>
        <asp:TextBox ID="TextBoxDataInicio" runat="server" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="form-group col-lg-2 col-md-5 col-sm-5 col-xs-12">
        <label for="hora_inicio">Hora Inicio</label>
        <asp:TextBox ID="TextBoxHoraInicio" runat="server" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="form-group col-lg-2 col-md-5 col-sm-5 col-xs-12">
        <label for="data_fim">Data Fim</label>
        <asp:TextBox ID="TextBoxDataFim" runat="server" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="form-group col-lg-2 col-md-5 col-sm-5 col-xs-12">
        <label for="hora_fim">Hora Fim</label>
        <asp:TextBox ID="TextBoxHoraFim" runat="server" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="form-group col-lg-4 col-md-12 col-sm-12 col-xs-12 text-align-right">
        <asp:LinkButton ID="LinkButton1" runat="server" class="btn btn-link pro-btn inline-button ajustar-lg" OnClick="LinkButton1_Click">Limpar</asp:LinkButton>
        <asp:Button ID="ButtonPesquisar" CssClass="btn btn-primary pro-btn inline-button ajustar-lg" runat="server" Text="Pesquisar" OnClick="ButtonPesquisar_Click" />
    </div>
</div>

I tried to implement the way you are commenting but is giving the error: String was not recognized as valid Datetime.

2 answers

3


look I made a test and rolled well, I noticed that it lacked the space in its concatenation!

look at: http://prntscr.com/4pbep5

the Cód:

 protected void Unnamed_Click(object sender, EventArgs e)
 {
    DateTime dataa;
    DateTime.TryParse(data.Text + " " + hora.Text, out dataa);

    Response.Write(dataa.ToString("hh:mm:ss - dd/MM/yyyy"));
 }

And on the aspx part:

<form id="form1" runat="server">
<div>
    <asp:TextBox runat="server" ID="hora" placeholder="hora"/>
    <br />
    <asp:TextBox runat="server" ID="data" placeholder="data"/>
    <br />
    <asp:Button Text="e vai!" runat="server" OnClick="Unnamed_Click"/>
</div>
</form>

UDPATE 001

I guess it’s because of out, try this:

var itens = macroController.ObterTodos(new Entities.FiltroMacro() { DataInicio = DateTime.Parse(TextBoxDataInicio.Text + " " + TextBoxHoraInicio.Text), DataFim = DateTime.Parse(TextBoxHoraFim.Text + " " + TextBoxHoraFim.Text) });

Be very careful with Cultures, Ui and Info. If you can’t post a comment in my reply, if I help you with a +1 and if you solve your problem, mark it as correct! Valeuu!

  • Can’t get the idea of your example and implement in my code.

  • Do so: DateTime dataa; DateTime.TryParse(TextBoxDataInicio.Text + " " + TextBoxHoraInicio.Text, out dataa);, when executing the values will be concatenated and played to the variable dataa.

  • In your example wheel cool, now in my code that is bone to implement.

  • take a look there... D

  • In the code passed beauty, but when I click the search button of my screen displays the question error. Would you have any more suggestions than I can make? Or would you like me to edit my question with more information

  • Dude, I think in my example I missed one more space that I forgot to put. Test again please!

  • So, I implemented the code and no longer presents the error. However, it is not filtering through the filter informed in the textbox.

  • 1

    It worked now, I had a wrong deal on my property Itemtemplate. It was worth too !!!

Show 3 more comments

1

Convert.ToDateTime() does not give date format options.

Utilize DateTime.TryParseExact:

CultureInfo cultura = new CultureInfo("pt-BR"); 
DateTime minhaData;
DateTime.TryParseExact(TextBoxDataInicio.Text + " " + TextBoxHoraInicio.Text, "dd/MM/yyyy hh:mm", cultura, DateTimeStyles.None, out minhaData);

minhaData will receive the value if the information is valid.

Browser other questions tagged

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