Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-11-18
10
Medium Priority
?
85,482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 7

Expert Comment

by:jj819430
ID: 9771476
You need to use smallDateTime
0
 
LVL 1

Author Comment

by:JElster
ID: 9771583
It is smalldatetime
0
 
LVL 15

Expert Comment

by:SRigney
ID: 9771614
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

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

Expert Comment

by:SRigney
ID: 9772094
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
 
LVL 15

Accepted Solution

by:
SRigney earned 180 total points
ID: 9772100
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
ID: 9772248
// 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
ID: 11229300
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
ID: 14120004
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
ID: 15116086
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

609 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