JElster
asked on
How do you format a SQL Server datetime to MM/DD/YYYY in WinForms text box
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.DataBindi ngs.Add(ne w Binding("Text",dS_RepInfo, "RepInfo.B irthDate") );
string bDate = dS_RepInfo.Tables["RepInfo "].Rows[0] ["BirthDat e"].ToStri ng();
this.dtBirthDate.Text = String.Format("{0:MM/dd/yy yy}",bDate );
this.dtBirthDate.DataBindi
string bDate = dS_RepInfo.Tables["RepInfo
this.dtBirthDate.Text = String.Format("{0:MM/dd/yy
You need to use smallDateTime
ASKER
It is smalldatetime
Try using a DateTime variable.
DateTime bDate = dS_RepInfo.Tables["RepInfo "].Rows[0] ["BirthDat e"];
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();
DateTime bDate = dS_RepInfo.Tables["RepInfo
this.dtBirthDate.Text = String.Format("{0}/{1}/{2}
Then you can deal with formating them however you want.
You can also use
this.dtBirthDate.Text = bDate.ToShortDateString();
ASKER
The databinding seems to override the formatting...
So How do I bind it?
So How do I bind it?
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
// 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.t extBoxDOB_ FormatDate );
dobBinding.Parse += new ConvertEventHandler(this.t extBoxDOB_ ParseDate) ;
textBoxDOB.DataBindings.Ad d(dobBindi ng);
// Format the Date Field to short date form for display in the TextBox
private void textBoxDOB_FormatDate(obje ct 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(objec t 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 );
}
}
// 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.t
dobBinding.Parse += new ConvertEventHandler(this.t
textBoxDOB.DataBindings.Ad
// Format the Date Field to short date form for display in the TextBox
private void textBoxDOB_FormatDate(obje
{
// 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(objec
{
// 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
}
}
this.dtBirthDate.DataBindi ngs.Add(ne w Binding("Text",dS_RepInfo, "RepInfo.B irthDate") );
//string bDate = dS_RepInfo.Tables["RepInfo "].Rows[0] ["BirthDat e"].ToStri ng();
//this.dtBirthDate.Text = String.Format("{0:MM/dd/yy yy}",bDate );
DateTime temp = System.Convert.ToDateTime( dS_RepInfo .Tables["R epInfo"].R ows[0]["Bi rthDate"]) ;
this.dtBirthDate.Text = temp.ToString("MM/dd/yyyy" );
//string bDate = dS_RepInfo.Tables["RepInfo
//this.dtBirthDate.Text = String.Format("{0:MM/dd/yy
DateTime temp = System.Convert.ToDateTime(
this.dtBirthDate.Text = temp.ToString("MM/dd/yyyy"
Hi
Try this
this.dtBirthDate.DataBindi ngs.Add(ne w Binding("Text",dS_RepInfo, "RepInfo.B irthDate") );
string bDate = dS_RepInfo.Tables["RepInfo "].Rows[0] ["BirthDat e"].ToStri ng();
Datetime dt = Datetime.Parse(bDate )
this.dtBirthDate.Text = dt.ToString(MM/dd/YYYY);
Mahesh
Try this
this.dtBirthDate.DataBindi
string bDate = dS_RepInfo.Tables["RepInfo
Datetime dt = Datetime.Parse(bDate )
this.dtBirthDate.Text = dt.ToString(MM/dd/YYYY);
Mahesh
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
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