Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > BOOK: Beginning C# 2005 Databases
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 2005 Databases
This is the forum to discuss the Wrox book Beginning C# 2005 Databases by Karli Watson; ISBN: 9780470044063
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 2005 Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 2nd, 2009, 04:20 PM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default Date format Problem

Hello,
I am using a MSAccess database date entered into the database are Medium date format ie. dd-mmm-yy format. But when the data is pulled from the db and shown into a datagrid, it's like mm/dd/yyyy hh:mm:ss. But I want only the medium date that is dd-mm-yy format(18-Aug-09). Please help.
Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old September 2nd, 2009, 08:02 PM
Authorized User
 
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
Default

If I remember it correctly, you could do this:

this.dataGridView1.Columns["MyDate"].DefaultCellStyle.Format = "dd-MMM-yy";

conversion example
Code:
        static void Test1()
        {
            DateTime today = DateTime.Now;

            string formattedDate = today.ToString("dd-MMM-yy");

            Console.WriteLine(formattedDate);
        }
Reply With Quote
The Following User Says Thank You to kokoness For This Useful Post:
skhan (September 3rd, 2009)
  #3 (permalink)  
Old September 2nd, 2009, 08:15 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You might also want to read this:
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=189

I wrote it, years ago, about ASP. But it's equally true of ASP.NET. *NO* format setting that you use in any database will carry over to the ASP/ASP.NET/JSP/PHP code. Read why in that FAQ.
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
skhan (September 3rd, 2009)
  #4 (permalink)  
Old September 3rd, 2009, 09:53 AM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default

Thank you for your reply, but how do I declare dataGridView1?.....I have a datagrid. but would it be like DataGridView dataGridView1= new DataGridView();

Everywhere they talk about this object but I can't find how to declare it? Please suggest.
Reply With Quote
  #5 (permalink)  
Old September 3rd, 2009, 10:14 AM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default

Thanks Old Pedant. The link was helpful. I get to know a lot about how it actually works.
Reply With Quote
  #6 (permalink)  
Old September 3rd, 2009, 10:58 AM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default

I am trying to format the date as follows:

Code:
foreach (DataRow r in ds.Tables[0].Rows)
 {
       string date1= r["First_Resurvey_Date"].ToString(); //works fine
       string date2= r["Last_Resurvey_Date"].ToString(); //works fine
       resultsLabel.Text = date1+"   "+date2;                 //works fine 

      //string dateString = r["First_Resurvey_Date"].ToString(); // Modified from MSDN [This doesn't work as sais invalid date]
        string format = "dd-MMM-yy";
        DateTime dateTime = DateTime.ParseExact("3/14/1985 12:00:00 AM", format, CultureInfo.InvariantCulture); [So, I have tried putting the data displayed as a string, but this doesn't work either]
 
         string date3= dateTime.ToString();
         NotesLabel.Text= date3;
         NotesLabel.Visible=true;

       //DateTime dt = Convert.ToDateTime(r["First_Resurvey_Date"]);
       //string format = "dd/MM/yy";
       //String date;
       //date = dt.ToString(format,DateTimeFormatInfo.InvariantInfo);

}
It says, "Exception: String was not recognized as a valid DateTime. "

My dates are currently displayed as "3/14/1985 12:00:00 AM", but I would like to have it as "14-Mar-85" or even "3/14/1985" would do as well.

Thanks in advance.
Reply With Quote
  #7 (permalink)  
Old September 3rd, 2009, 01:28 PM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default

Now I could change the data format into how I wanted it to be but I don't understand how to insert that into the Datagrid to display as such??

Code:
[ds is my dataset]
foreach (DataRow r in ds.Tables[0].Rows)
{
     string dateString1 = r["First_Resurvey_Date"].ToString(); 
    DateTime dateTime=Convert.ToDateTime(dateString1);
    string [] dateStringArray= dateTime.GetDateTimeFormats();

    r["First_Resurvey_Date"]= dateStringArray[6]; [This doesn't work, I cannot just assign the specific value to a datagrid row/cell......I need help on how to do that] [dateStringArray[6], gives the date format as dd-MMM-YY, which is how I want to display my date in the application]
                       
    NotesLabel.Text= dateStringArray[6];
    NotesLabel.Visible=true;
 }
DataView view = ds.Tables[0].DefaultView;
resultsDatagrid.DataSource = view;
resultsDatagrid.DataBind();
resultsDatagrid.Visible = true;
Thank you .
Reply With Quote
  #8 (permalink)  
Old September 3rd, 2009, 05:20 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Kokoness showed you the answer in his post.

And WHY do you get the value out of the database *AS A STRING* if it's a DateTime value there???

Code:
foreach (DataRow r in ds.Tables[0].Rows)
{
    DateTime dt = r["First_Resurvey_Date"]; // do NOT convert it to string and then BACK to datetime!
    NotesLabel.Text = td.ToString("dd-MMM-yy"); 
    NotesLabel.Visible=true;
 }
...
Though why you would loop through all the rows and, in each row, set just the one label to the date, I don't get, at all.

I suspect that all you need is code to change the display of the value from the datatable *during* the binding to the datagrid. No?

But if you don't know how to do that, then maybe the right answer is to change your SQL query to get the date *AS A STRING* from Access.

Like this:
Code:
SELECT Right('0' & Day(yourfield),2) & '-' & MonthName(Month(yourfield),True) & '-' & Right(CStr(Year(yourfield)),2) AS formattedDate, ...
Reply With Quote
  #9 (permalink)  
Old September 3rd, 2009, 05:33 PM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default

I had to do that stupid thing cause when I tried to do like the following:
DateTime dd=r["First_Resurvey_Date"];

It gave me the following error:
'System.Data.DataColumn.DataType' denotes a 'property' where a 'method' was expected

The only methods associated with r["First_Resurvey_Date"] were Equals(), getHashCode(), getType() and ToString(). So to get the DateTime value from column "First_Resurvey_Date", I had to do those meaningless steps. I could be completely wrong as I have very little knowledge of ASP.NET C#. But that's what I could do.

Yes I needed code to change the display of the value from the datatable *during* the binding to the datagrid. I don't understand how should I do that, so maybe I will try out the formatting in the SQL Query. Thank you for your suggestion. And sorry for my weird/stupid questions. It's not that I am not doing proper research, I am trying my best. It's just that I don't know everything about DataGrid properties to try out.
Reply With Quote
  #10 (permalink)  
Old September 3rd, 2009, 05:59 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

To change the format, you have to specify custom binding with <%#...%> tags. I'm not really an ASP.NET person, so I'm hesitant to try to lead you here.

Ohh...and the problem is that you aren't using a "strongly typed dataset" so indeed you must *CAST* the Object (which is what r{"xxx"] gives you...a generic Object) to the proper type.

So *probably* just
Code:
    DateTime dt = (DateTime) r["First_Resurvey_Date"];
But not a C# person, either (Java and C++), so you might have to use an explicit convert function.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Date format problem abhishekkashyap27 C# 2005 1 June 23rd, 2008 02:25 AM
Date Format Problem cyberddindia Classic ASP Basics 2 October 23rd, 2006 01:49 AM
Date Format Problem Scripts82 Access VBA 2 March 13th, 2006 08:36 PM
Date format problem ERC Crystal Reports 1 June 15th, 2005 04:03 AM
Date format problem widad Classic ASP Basics 1 October 15th, 2004 06:31 PM



All times are GMT -4. The time now is 01:51 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.