Solved

How do you format a SQL Server datetime to MM/DD/YYYY in WinForms text box

Posted on 2003-11-18
10
85,389 Views
Last Modified: 2012-05-04
I'm trying to format a date in a text box (birthday)... but it still displays the time ? any idea what is wrong...thanks..      

this.dtBirthDate.DataBindings.Add(new Binding("Text",dS_RepInfo,"RepInfo.BirthDate"));
string bDate = dS_RepInfo.Tables["RepInfo"].Rows[0]["BirthDate"].ToString();
this.dtBirthDate.Text = String.Format("{0:MM/dd/yyyy}",bDate);
0
Comment
Question by:JElster
10 Comments
 
LVL 7

Expert Comment

by:jj819430
Comment Utility
You need to use smallDateTime
0
 
LVL 1

Author Comment

by:JElster
Comment Utility
It is smalldatetime
0
 
LVL 15

Expert Comment

by:SRigney
Comment Utility
Try using a DateTime variable.

DateTime bDate = dS_RepInfo.Tables["RepInfo"].Rows[0]["BirthDate"];
this.dtBirthDate.Text = String.Format("{0}/{1}/{2}", bDate.Month, bDate.Day, bDate.Year);


Then you can deal with formating them however you want.

You can also use
this.dtBirthDate.Text = bDate.ToShortDateString();
0
 
LVL 1

Author Comment

by:JElster
Comment Utility
The databinding seems to override the formatting...
So How do I bind it?
0
 
LVL 15

Expert Comment

by:SRigney
Comment Utility
you may be able to do it within the select.  
use a convert on the date to a string.
But that may also give you problems as you go to update the field.

At my job we have a layer between the database and the desktop, so we take the data and put it into a different object that has it's own formatting functionality.  This is an object built specifically for each database.

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Accepted Solution

by:
SRigney earned 60 total points
Comment Utility
You may also be better unbinding the date field and dealing with manually setting it's value into the field when things change.
0
 
LVL 1

Author Comment

by:JElster
Comment Utility
// We want to format the DateOfBirth, so process the format and
// parse events for the DateOfBirth text box. To accomplish this,
// we add our own event handlers to the Format and Parse events.
Binding dobBinding = new Binding("Text", custList, "DateOfBirth");
dobBinding.Format += new ConvertEventHandler(this.textBoxDOB_FormatDate);
dobBinding.Parse += new ConvertEventHandler(this.textBoxDOB_ParseDate);
textBoxDOB.DataBindings.Add(dobBinding);

// Format the Date Field to short date form for display in the TextBox
private void textBoxDOB_FormatDate(object sender, ConvertEventArgs e)
{
    // We only deal with converting to strings from dates
    if (e.DesiredType != typeof(string)) return ;
    if (e.Value.GetType() != typeof(DateTime)) return ;

    DateTime dt = (DateTime)e.Value;
    e.Value = dt.ToShortDateString();
}

// Parse the textbox contents and turn them back into a date
private void textBoxDOB_ParseDate(object sender, ConvertEventArgs e)
{
    // We only deal with converting to dates and strings
    if (e.DesiredType != typeof(DateTime)) return;
    if (e.Value.GetType() != typeof(string)) return;

    string value = (string)e.Value;

    try
    {
        e.Value = DateTime.Parse(value);
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

0
 
LVL 8

Expert Comment

by:vinhnl
Comment Utility
this.dtBirthDate.DataBindings.Add(new Binding("Text",dS_RepInfo,"RepInfo.BirthDate"));
//string bDate = dS_RepInfo.Tables["RepInfo"].Rows[0]["BirthDate"].ToString();
//this.dtBirthDate.Text = String.Format("{0:MM/dd/yyyy}",bDate);
DateTime temp = System.Convert.ToDateTime(dS_RepInfo.Tables["RepInfo"].Rows[0]["BirthDate"]);
this.dtBirthDate.Text = temp.ToString("MM/dd/yyyy");
0
 

Expert Comment

by:mahesh_r_sharma
Comment Utility
Hi

Try this
this.dtBirthDate.DataBindings.Add(new Binding("Text",dS_RepInfo,"RepInfo.BirthDate"));
string bDate = dS_RepInfo.Tables["RepInfo"].Rows[0]["BirthDate"].ToString();
Datetime dt = Datetime.Parse(bDate )
this.dtBirthDate.Text = dt.ToString(MM/dd/YYYY);

Mahesh
0
 
LVL 6

Expert Comment

by:dash420
Comment Utility
simply solution to this is do the formating in the SQL Server while select the birth date.

something like  

select convert( nvarchar,getdate(),101)  

this is dateformat in MM/DD/YYYY for the system date, change this query to corresponding column
of you database table.

select convert( nvarchar,BirthDate,101)  from RepInfo

let me know if it solve your problem.

regards,
Dash
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now