Link to home
Start Free TrialLog in
Avatar of bidgadget
bidgadgetFlag for United States of America

asked on

Format date label field in Visual Studio and VB.net

Visual Studio 2015 using vb.net I have a label that is displaying a date field from a database.  On the form the date shows up as Nov 20 2018 and I need it to show up as November 20, 2018

Is there a way to format the field to show the correct date format?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

What is your current code?
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bidgadget

ASKER

Thank you. That worked
Actually I am trying to get the date value in the database files. Your solution is giving current date.

How would I get the current data entered in the field in that format?  Thank you
A database should always store a date in a date/datetime format. In order to save a date using a very specific format, you would to store it as a string and you really don't want that.

Once a value is stored into the database, it belongs to the client app/reporting to format it the way you really it
So there is no way then to create another label field and take the date in the database and format it the way I want?
where are you trying to show that date?
On a letter that will pull the date entered into the database field. I am able to get it to show for example Oct 30 2018 but I need it to show October 30, 2018
How do you produce that letter? If you pass the value from your application to the letter, you can use the same syntax:
yourobjectonyourletter = yourfieldfromthedatabase.ToString("MMMM d, yyyy")

Open in new window


or if you letter is produced using a reporting engine, there is surely a field format feature
Just producing the letter off a form in asp.net in visual studio
That doesn't tell me what kind of object you are trying to feed. I really want to help you but you also need to help me.
so sorry.

I have a sql database table.  I have a field called confirm_date_instructed.  It is a datetime field.

I have a form that I am using as a letter.   The sentence is.  "your date instructed of _______    The __________ is supposed to be the database filed of Confirm_date_instrcted.    The contents of the field are entered as a date in the process before this field.  I want the field to show up as a full date.  For example if the date was entered in the prior form and submitted to the database was 10/30/2018, I would want it to show as October, 30, 2018

I have unprotected the field by using
   Dim CONFIRM_DATE_instructed As Label = CType(FormView1.FindControl("CONFIRM_DATE_instructedLabel"), Label)

then I want to convert the field
 CONFIRM_DATE_instructed.Text = CONFIRM_DATE_instructed.ToString("MMMM d, yyyy")

I am getting the error
An exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll but was not handled in user code

Additional information: Conversion from string "MMMM d, yyyy" to type 'Integer' is not valid.
Hum!!!! You have that full sentence in the label and you want to extract the date?

Because in your code, you are saying CONFIRM_DATE_instructed = CONFIRM_DATE_instructed. This is not logical!
Sorry. That was just a trial. Not sure how I would do it
what do you have in your CONFIRM_DATE_instructedLabel.Text? In which object (dataset or other) is your datetime field?
the Datetime field is the Confirm_date_instructed which is part of the dataset.

The below code hives me mm/dd/yyyy  but need it to be full date ie october 30, 2018

CONFIRM_DATE_instructed.Text = CONFIRM_DATE_instructed.Text.Substring(0, CONFIRM_DATE.Text.Length - 7)
it would be much safer to go back to the dataset :
CONFIRM_DATE_instructed.Text =directcast( yourdataset.tables(yourtableid).rows(yourrowid).item("yourfieldname"), datetime).ToString("MMMM d, yyyy")

Open in new window