Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String.Format is not working 1

Status
Not open for further replies.

seaport

MIS
Jan 5, 2000
923
US
I got a date field in the DataTable. I tried to reformat it with String.Format method so the data appears as "6/13/2006" instead of "6/13/2006 12:00:00 AM" in a bound column in a datagrid. Here are the code to change the data in the DataTable before it is bound to a DataGrid.
Code:
           row.BeginEdit()
            row.Item("ActivityDate") = String.Format("{0:d}", row.Item("ActivityDate"))
            row.EndEdit()

The application is built without any error. However, the page still shows "6/13/2006 12:00:00 AM" format.

In the debug mode, I got the following message in the Immediate Window.

Code:
?String.Format("{0:d}", row.Item("ActivityDate"))
The parameter is incorrect. 
?string.format("{0:d}",datetime.now())
The parameter is incorrect.

However, when I started another visual studio and create a console application and stoped in the debug mode, I got the following in the Immediate Window.
Code:
?string.format("{0:d}",datetime.now())
"6/20/2006"

Frankly, I am totally lost.

Any suggestions?

Thanks in advance.

Seaport
 
Does this work in your immediate window?

Code:
?System.DateTime.Now.ToString("M/d/yyyy")
 
in your html try this..

Container, "DataItem.YourDateField", "{0:MM/dd/yyyy}")

or using a reader

GetDateTime(0).ToShortDateString

 
No. I got the same message.

The parameter is incorrect.

By the way, in the same "Command Window - Immediate", the following works.
Code:
?datetime.now()
#6/20/2006 3:38:46 PM#

Seaport
 
I just found a way to go around the problem by specifying the formatting string in aspx file (not the Code-Behind vb file)
Code:
 DataTextFormatString="{0:d}"

However, I still cannot figure out those strange messages from the Immediate Window. I am using Visual Studio 2002 and .net framework 1.1.

Seaport

 
Something fishy looking is that the column of the DataTable appears to store a DateTime, but in your code, you're trying to store a string (returned by String.Format).

Setting the DataTextFormatString of a ListControl will result in the proper output because the DateTime ends up as a string when used by the control.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top