How to format a date in a textbox as dd/MMMM/yyyy in vb.net

  • Thread starter Thread starter wirejp
  • Start date Start date
W

wirejp

Guest
Hi All,

I am using a MySQL database which is linked to Visual Studio Community Edition 2017. In the MySQL database, the date field appears as: 2017-10-20, while in Visual Studio UI, I have a textbox is populated with the date as: 10/20/2017 12:00:00 AM. How do I format the date as 20/10/2017 in textbox in the Visual Studio UI?

I tried used this vb.net code below but I received an Unhandled exception error message at the line: -

txtBookSigningDate.Text = Format(Convert.ToDateTime(txtBookSigningDate.Text+Now.Year.ToString(), sFormat), "dd/MM/yyyy")



VB.net code is:


Dim conn As String
Dim sFormat As System.Globalization.DateTimeFormatInfo = New System.Globalization.DateTimeFormatInfo()
sFormat.ShortDatePattern = "dd/MM/yyyy"
txtBookSigningDate.Text = Format(Convert.ToDateTime(txtBookSigningDate.Text+Now.Year.ToString(), sFormat), "dd/MM/yyyy")
conn= "server=localhost;Port=3306;database=authors;User ID=root;Password=mypassword"

Dim ConnSQL As MySqlConnection = New MySqlConnection(conn)
CmdSQLSelect = "Select * from Authors"
da5 = New MySqlDataAdapter(CmdSQLSelect, ConnSQL)
Dim SQLCommandBuild As MySqlCommandBuilder = New MySqlCommandBuilder(da)
da5.Fill(dt)

'Bind the DataTable to the UI via a BindingSource.
BindingSource.DataSource = dt
Me.BindingNavigator.BindingSource = Me.BindingSource

txtBookSigningDate.DataBindings.Add("Text", Me.BindingSource, BookSigningDate")



The Unhandled Exception error message: -
Exception Unhandled

System.FormatException: 'String was not recognized as a valid DateTime.'

System.FormatException
HResult=0x80131537
Message=String was not recognized as a valid DateTime.
Source=mscorlib
StackTrace:
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.Convert.ToDateTime(String value, IFormatProvider provider)
at Authors_Database.Form1.Form1_Load(Object sender, EventArgs e) in F:\Authors Folder\Visual Studio 2017 Professional (Community Edition)\Authors\Authors Database\Authors Database\Form1.vb:line 338
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Thank you in advance

Continue reading...
 
Back
Top