Fill TextBox with TextMode = Date of Asp.Net from Date SQL

0

I would like to know how I can fill a "Date" type textbox with a date obtained from a SQL Server table

Currently I have it like this

 txtFecha.Text = (((DateTime)firstTable.Rows[0]["FechaCredito"]).ToString("dd/MM/yyyy")).ToString(); 

But when it comes to viewing it from the form, it shows me as if nothing had been loaded, instead of filling it, like this:

I am grateful in advance

    
asked by Ignacio Gabriel 02.07.2018 в 21:50
source

1 answer

0

One solution can be to convert the query into a DateTime type object and then assign it to the textBox formatting it with date:

  Dim conn As New SqlConnection()
    conn = New SqlConnection(conexion)
    conn.Open()
    Dim query As String = "SELECT * FROM TABLA WHERE DATO = @DATO"
    Dim cmd As New SqlCommand(query, conn)
    cmd.Parameters.AddWithValue("@DATO", (txtDato.Text))
    Dim dr As SqlDataReader = cmd.ExecuteReader()
    If dr.Read() Then
        Dim dt As DateTime = Convert.ToDateTime(dr.Item("FechaCredito").ToString())
        txtFecha.Text = String.Format("{0:yyyy-MM-dd}", dt)
    End If

I do not know if it's the best way, but it worked for me.

    
answered by 24.01.2019 в 00:21