Format Textbox with date

Asked

Viewed 188 times

1

I have here a form with several textbox and some combobox. What happens when I insert a code and fill the textbox with birth date appears to me in this format 01/01/1900 00:00:00 and I just want the date to appear.

Can you help me? the names that appear there as Doctorbasic and Doctorenterprisedetails are table names and the DBO is the column with birth dates.

Public Class Form1

    Public Sub TxtCodigoMed_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TxtCodigoMed.KeyPress


        If e.KeyChar.ToString = ChrW(Keys.Enter) Then ' Enter para pesquisar
            TxtCodigoMed.Focus()
            e.Handled = True

            CarregarDados()

        End If


    End Sub


    Public Sub CarregarDados()
        'LblVazia.Text = 
        Dim cnnstr As SqlConnection = New SqlConnection("Data Source=****;Initial Catalog=***;user=**;password=****")
        Dim myCommand As String
        Dim cmd As SqlCommand
        Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
        Dim table = New DataTable


        myCommand = "SELECT * FROM DoctorBasic INNER JOIN DoctorEnterpriseDetails ON DoctorBasic.DoctorId = DoctorEnterpriseDetails.DoctorId WHERE EnterpriseId = 26 AND Code = " & TxtCodigoMed.Text

        cnnstr.Open()
        cmd = New SqlCommand(myCommand, cnnstr)
        da.SelectCommand = cmd
        da.Fill(table)

        TxtPrimeiroNome.Text = table.Rows(0)("FirstName").ToString
        TxtApelido.Text = table.Rows(0)("LastName").ToString
        TxtNomeComp.Text = table.Rows(0)("FullName").ToString
        TxtDataNasc.Text = table.Rows(0)("DOB").ToString
        TxtApelido.Text = table.Rows(0)("LastName").ToString
        TxtDataForm.Text = table.Rows(0)("GraduateDate").ToString
        TxtGrauForm.Text = table.Rows(0)("Degree").ToString
        TxtTelefone.Text = table.Rows(0)("Telephone").ToString
        TxtTelem.Text = table.Rows(0)("Mobile").ToString
        TxtMail.Text = table.Rows(0)("Emailid").ToString
        TxtObs.Text = table.Rows(0)("Observation").ToString
        TxtLusov.Text = table.Rows(0)("Lusov").ToString
        Me.DoctorsAddressTableAdapter.Fill(Me.GSM81DataSet.DoctorsAddress, Me.TxtCodigoMed.Text.ToString) 'carrega a grid através da pesquisa do codigo do médico
        cnnstr.Close()


    End Sub


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'GSM81DataSet.GroupType' table. You can move, or remove it, as needed.
        Me.GroupTypeTableAdapter.Fill(Me.GSM81DataSet.GroupType)
        'TODO: This line of code loads data into the 'GSM81DataSet2.Rank' table. You can move, or remove it, as needed.
        Me.RankTableAdapter.Fill(Me.GSM81DataSet2.Rank)
        'TODO: This line of code loads data into the 'GSM81DataSet1.PrescibingPotential' table. You can move, or remove it, as needed.
        Me.PrescibingPotentialTableAdapter.Fill(Me.GSM81DataSet1.PrescibingPotential)
        'TODO: This line of code loads data into the 'GSM81DataSet.Speciality' table. You can move, or remove it, as needed.
        Me.SpecialityTableAdapter.Fill(Me.GSM81DataSet.Speciality)

    End Sub

    Public Sub GravarDoctorBasic()

        Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=****;Initial Catalog=***;user=**;password=***")
        Dim myCommand As String
        Dim cmd As SqlCommand


        myCommand = "UPDATE DoctorBasic SET " & _
        "Lusov = '" & TxtLusov.Text & "', " & _
      "FirstName = '" & TxtPrimeiroNome.Text & "', " & _
      "LastName = '" & TxtApelido.Text & "', " & _
      "FullName = '" & TxtNomeComp.Text & "', " & _
       "DOB = " & TxtDataNasc.Text.ToString & ", " & _
      "GraduateDate = '" & TxtDataForm.Text.ToString & "', " & _
      "Emailid = '" & TxtMail.Text & "', " & _
       "Degree = '" & TxtGrauForm.Text & "', " & _
       "Observation = '" & TxtObs.Text & "' " & _
      "WHERE DoctorId = " & TxtCodigoMed.Text.ToString

        cmd = New SqlCommand(myCommand, myConnectionString)
        MsgBox(myCommand)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()


    End Sub

    Public Sub GravarDoctorEnterpriseDetails()
        Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=****;Initial Catalog=****;user=****;password=*******")
        Dim myCommand As String
        Dim cmd As SqlCommand

        myCommand = "UPDATE DoctorEnterpriseDetails SET " & _
                 "RankId = " & CBSelec.ValueMember & ", " & _
                 "GroupId = " & CBCateg.ValueMember & ", " & _
                 "PrescribingPotential = " & CBPP.ValueMember & ", " & _
                 "Telephone = " & TxtTelefone.Text & ", " & _
                 "Mobile = " & TxtTelem.Text & ", " & _
                 "Speciality1 = " & CBEsp1.ValueMember & ", " & _
                 "Speciality2 = " & CBEsp2.ValueMember & " " & _
                  "WHERE DoctorId = " & TxtCodigoMed.Text.ToString

        cmd = New SqlCommand(myCommand, myConnectionString)
        ' MsgBox(myCommand)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub

    Public Sub btnGravar_Click(sender As System.Object, e As System.EventArgs) Handles btnGravar.Click
        GravarDoctorBasic()
        GravarDoctorEnterpriseDetails()
    End Sub
End Class

1 answer

2


You can use the function Substring:

TxtDataForm.Text = table.Rows(0)("GraduateDate").ToString().Substring(0, IIf(table.Rows(0)("GraduateDate").ToString().Length > 10, 10, table.Rows(0)("GraduateDate").ToString().Length))

Or making Casting:

TxtDataForm.Text = CType(table.Rows(0)("GraduateDate").ToString(), DateTime).ToShortDateString()
  • used the first one and it worked perfectly! Thanks, I’m also having trouble doing update. I have to open a new topic to ask for help?

  • 1

    It would be good, as it separates matters.

  • there’s just one small problem.. gives me this error in the substring line " The index and the length must reference a location in the string. Parameter name: length " (this appeared to me when I entered a doctor with no birth date)

  • Check now, I edited the reply with a check.

  • yes that’s right!

Browser other questions tagged

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