הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, September 13, 2018 8:34 PM
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
All replies (8)
Friday, September 14, 2018 4:49 PM ✅Answered
Hi
Not exsactly sure that this is helpful, but here it is.
This is a stand alone example using a DataGridView1 on Form1 to display the DataTable data. The DataTable has some dummy data and the DataGridView is set to show the Date column as MM/dd/yyyy.
There is also a TextBox named txtBookSigningDate, and it is bound to the same binding source of the DataGridView (which is bound to the DataTable via a BindingSource) - however, the Date in this TextBox is displayed as dd/MMM/yyyy - I think I am aiming in the correct general direction with this - maybe?
Try this code out to see if it is anywhere in the ballpark :)
' Form1 with DataGridView1
' and TextBox named txtBookSigningDate
Option Strict On
Option Explicit On
Public Class Form1
Dim dt As New DataTable
Dim BS As New BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' make some dummy test data
With dt
.Columns.Add("One", GetType(Integer))
.Columns.Add("Two", GetType(String))
.Columns.Add("Three", GetType(Boolean))
.Columns.Add("Date", GetType(DateTime))
.Rows.Add(1, "Fred", True, Now.AddDays(-20))
.Rows.Add(2, "Mary", False, Now.AddDays(-10))
.Rows.Add(11, "Brian", True, Now.AddDays(31))
.Rows.Add(111, "Elizabeth", True, Now.AddDays(200))
End With
BS.DataSource = dt
DataGridView1.DataSource = BS
With DataGridView1
' set to MM/dd/yyyy
.Columns("Date").DefaultCellStyle.Format = "MM/dd/yyyy"
End With
Dim dbind As New Binding("Text", BS, "Date")
dbind.FormattingEnabled = True
'display in TB as dd/MM/yyyy
dbind.FormatString = "dd/MM/yyyy"
txtBookSigningDate.DataBindings.Add(dbind)
End Sub
End Class
Regards Les, Livingston, Scotland
Thursday, September 13, 2018 9:48 PM | 1 vote
Hi
Noty entirely sure what you want as the question title contains a request that differs from the message body.
Here is a DateTime displayed in two of the formats mentioned, put in TextBox1 and TextBox2
Dim d As DateTime = Now
TextBox1.Text = d.ToString("dd/MMMM/yyyy")
TextBox2.Text = d.ToString("MMMM/dd/yyyy")
Regards Les, Livingston, Scotland
Friday, September 14, 2018 7:09 AM
Hi Les,
Thank you for the feedback. I applied the vb.net code:
Dim d As DateTime = Now
TextBox1.Text = d.ToString("dd/MMMM/yyyy")
This vb.net code shows the current date in the textbox. However, I would like to display the date field in the Authors table from the MySQL database in the Visual Studio UI,rather than displaying the current date.
Friday, September 14, 2018 11:55 AM
Hi
In the example I posted, I used Now as the Datetime - in your case you would use the DateTime from your DataTable/DB etc
Regards Les, Livingston, Scotland
Friday, September 14, 2018 2:07 PM
Hi Les,
Ah, I see. You mean that I should hardcode the DateTime from my DataTable/DB etc. e.g.
Dim d As DateTime = "10-20-2017"
txtBookSigningDate.Text = d.ToString("dd-MMMM-yyyy")
I would like to dynamically display the TimeDate from the MySQL database in the Visual Studio UI and in turn if I choose to change the DateTime in the Visual Studio UI, the updated TimeDate will be reflected in the MySQL database.
Thanks
Friday, September 14, 2018 2:30 PM
Hi Les,
Ah, I see. You mean that I should hardcode the DateTime from my DataTable/DB etc. e.g.
Dim d As DateTime = "10-20-2017" txtBookSigningDate.Text = d.ToString("dd-MMMM-yyyy")
I would like to dynamically display the TimeDate from the MySQL database in the Visual Studio UI and in turn if I choose to change the DateTime in the Visual Studio UI, the updated TimeDate will be reflected in the MySQL database.
Thanks
Hi
Not quite what I meant.
Is the DateTime you want to display in a column of the datatable?
If so, which column?
Regards Les, Livingston, Scotland
Friday, September 14, 2018 3:59 PM
Hi Les,
Yes, I want to display the DateTime in a column of the datatable. It is the column 3.
Thanks.
Friday, September 14, 2018 6:06 PM
Hi Les,
It works great! Thanks a lot.
Cheers.