String was not recognized as a valid DateTime

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, however I am looking to search my data through the information entered 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 you are giving the error: String was not recognized as a valid DateTime.

    
asked by anonymous 22.09.2014 / 20:31

2 answers

3

I did a test and it turned out good, I noticed that it lacked space in its concatenation!

look at: link

the code:

 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 in 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 pulled out should be 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 not post a comment there in my answer, if you help a +1 and solve your problem mark it as correct! Thanks!

    
22.09.2014 / 20:49
1

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

Use 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.

    
22.09.2014 / 20:51