How to show a datetime field in a TextBox

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I have a datetime filed, name Birthday.
I use aTexBox.Text= ((DateTime)dr["Birthday"]).ToShortDateString();
but it result in an .InvalidCastException:

How can I datetime field in a TextBox?
 
I have a datetime filed, name Birthday.
I use aTexBox.Text= ((DateTime)dr["Birthday"]).ToShortDateString();
but it result in an .InvalidCastException:

How can I datetime field in a TextBox?

What type is the Birthday column, and does it allow nulls?
 
ad said:
I have a datetime filed, name Birthday.
I use aTexBox.Text= ((DateTime)dr["Birthday"]).ToShortDateString();
but it result in an .InvalidCastException:

That's because dr["Birthday"] doesn't evaluate to a DateTime instance
(or more precisely, a boxed DateTime struct).
How can I datetime field in a TextBox?

Like you are doing. Try printing dr["Birhtday"].GetType().FullName :)
 
hi,


what is dr ? it's a DataRow or a DataReader ?

what is the type of "BirthDay"column?

Does it support null ?

cheers,
 
Thank
1. dr is a DataReader
2. the type of "BirthDay"column is DateTime
3. it support null

Ignacio Machin ( .NET/ C# MVP ) said:
hi,


what is dr ? it's a DataRow or a DataReader ?

what is the type of "BirthDay"column?

Does it support null ?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


ad said:
I have a datetime filed, name Birthday.
I use aTexBox.Text= ((DateTime)dr["Birthday"]).ToShortDateString();
but it result in an .InvalidCastException:

How can I datetime field in a TextBox?
 
Hi,

If it does support null you have to check for that:

textbox.Text = dr["Birthday"] == DBNull.Value? " N/A" :
((DateTime)dr["BirthDay"]).ToShortDateString();

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


ad said:
Thank
1. dr is a DataReader
2. the type of "BirthDay"column is DateTime
3. it support null

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
¼¶¼g©ó
¶l¥ó·s»D:OCH#[email protected]...
hi,


what is dr ? it's a DataRow or a DataReader ?

what is the type of "BirthDay"column?

Does it support null ?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


ad said:
I have a datetime filed, name Birthday.
I use aTexBox.Text= ((DateTime)dr["Birthday"]).ToShortDateString();
but it result in an .InvalidCastException:

How can I datetime field in a TextBox?
 
Back
Top