5 Replies - 972 Views - Last Post: 02 December 2012 - 01:37 PM Rate Topic: -----

#1 dsharp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-December 12

populate textbox using hashtable

Posted 02 December 2012 - 02:49 AM

I'm creating an agenda program using windows form application, I'm trying to load data from a file into a hash table and display this data into text boxes. The labels on the text boxes are dates but these dates are of the form (dd MMMM dddd e.g 30 November Friday) and are used as key values for the hash table. The dates in the file are of the form (dd MM yyyy). A sample of the data in the file is shown.

10/07/2012,Will go to swimming at 10.30 *

30/11/2012,Will go to swimming at 15.30 *

When I load the form, the data should be shown in the corresponding textbox. For example Will go to swimming at 15.30 * must be shown under text box with label "30 November Friday" (because it corresponds to date 30/11/2012). My code so far is this:-

  while ((line = sr.ReadLine()) != null)
             {
                 string[] inData = line.Split(',');
                 key = inData[0];
                 value = inData[1];
                 hT.Add(key, value);

                 foreach (DictionaryEntry dataEntry in hT)
                 {
                     //Will throw a FormatException if 'dataEntry.Key' doesn't represent a valid date format
                     
                     DateTime keyAsDate = Convert.ToDateTime(key);

                     //This overload of 'ToString' uses CurrentCulture
                     string comparisonKey = keyAsDate.ToString("dd MMMM dddd");
                     
                     if (label1.Text.Equals(comparisonKey, StringComparison.OrdinalIgnoreCase))
                     {
                         textBox1.Text = dataEntry.Value.ToString();
                        
                     }

                     if (label2.Text.Equals(comparisonKey, StringComparison.OrdinalIgnoreCase))
                     {
                        
                         textBox2.Text = dataEntry.Value.ToString();
                        
                     }

                     if (label3.Text.Equals(comparisonKey, StringComparison.OrdinalIgnoreCase))
                     {
                         textBox3.Text = dataEntry.Value.ToString();
                         
                     }




Nothing is displayed after i load the form. The textBoxes are empty. I just can't seem to place the error. How can i fix this?

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: populate textbox using hashtable

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: populate textbox using hashtable

Posted 02 December 2012 - 09:09 AM

Yikes! That is a terrible way to do things, but that's the code you have now. Have you tried setting a breakpoint on line 17 and inspecting the values of keyAsDate and comparisonString?

Also on line 15, you have "dddd". Did you mean to use "yyyy" ?
Was This Post Helpful? 0
  • +
  • -

#3 dsharp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-December 12

Re: populate textbox using hashtable

Posted 02 December 2012 - 09:51 AM

No it's actually supposed to be "dddd" not "yyyy" thats why there's a conversion there.
I did insert a break point earlier and it turns out that "key" and "keyasdate" don't have the same values at an instance..but i don't why that is.. couldn't figure it out
Can you see any errors in the code or a better approach to the problem in general?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: populate textbox using hashtable

Posted 02 December 2012 - 10:07 AM

If key and keyAsDate aren't matching up, that means the default parsing isn't working with the default locale that you are running in versus the data format stored in the file. Use DateTime.Parse(), DateTime.ParseExact(), or DateTime.TryParse() and pass in a specific format and culture to get the data in a form you want.
Was This Post Helpful? 0
  • +
  • -

#5 dsharp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-December 12

Re: populate textbox using hashtable

Posted 02 December 2012 - 11:08 AM

But isn't that what the line 15 is doing? i.e converting the date on the file to match that of the labels?
Also the date format on the file matches the system date.

I put a messageBox on line 16 and noticed that during the iterations some key values read from the file (i.e the dates) are used more than once when rather each value is supposed to be used once in the while loop and in the "foreach statement" loop
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: populate textbox using hashtable

Posted 02 December 2012 - 01:37 PM

View Postdsharp, on 02 December 2012 - 01:08 PM, said:

But isn't that what the line 15 is doing? i.e converting the date on the file to match that of the labels?
Also the date format on the file matches the system date.

Yes. But when you read the date on line 1, get the date on line 4, and the try to parse the date into keyAsDate on line 12, there is no guarantee that the system locale matches the UI locale. The .NET framework allows the UI local to be set to French Canada, while the system locale is set to English United States. The date parsing for some locales have the day before the month.

View Postdsharp, on 02 December 2012 - 01:08 PM, said:

I put a messageBox on line 16 and noticed that during the iterations some key values read from the file (i.e the dates) are used more than once when rather each value is supposed to be used once in the while loop and in the "foreach statement" loop

That seems to because of your call to add the same date to hash table on line 6. Depending on your hash table implementation, the call to add maybe allowed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1