Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello guys,

I need to get the months and insert it to the database. I have two datetimepickers. the first one is the start date and the other one is the end date. When I click the button, it will get the list of months based from the two datetimepicker and save it to the database.

Here is what I have tried:(I got an error in .ParseExact saying no overload method for parseExact takes one argument). I do not understand this. It is my first time to work with this datetime :(


public static IEnumerable<Tuple<string, int>> MonthsBetween(
        DateTime startDate,
        DateTime endDate)
     {

         DateTime iterator;
         DateTime limit;

         if (endDate > startDate)
         {
             iterator = new DateTime(startDate.Year, startDate.Month, 1);
             limit = endDate;
         }
         else
         {
             iterator = new DateTime(endDate.Year, endDate.Month, 1);
             limit = startDate;
         }

         var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
         while (iterator <= limit)
         {
             yield return Tuple.Create(
                 dateTimeFormat.GetMonthName(iterator.Month),
                 iterator.Year);
             iterator = iterator.AddMonths(1);
         }
     }



C#
private void button1_Click_1(object sender, EventArgs e)
    {
        string a = dateTimePicker1.ToString();
        string b =dateTimePicker2.ToString();

        var startDate = DateTime.ParseExact(a);
        var endDate = DateTime.ParseExact(b);

        var months = MonthsBetween(startDate, endDate);
    }
Posted
Updated 18-Jan-15 13:18pm
v2
Comments
SrgjanX 18-Jan-15 18:53pm    
i think you need to give some more info, for example what kind of database,
i can help you with the connection if its sql or mysql
DarkDreamer08 18-Jan-15 19:19pm    
I updated my question. i tried something but I got some errors. Hope you help me here, please.
Sergey Alexandrovich Kryukov 18-Jan-15 19:04pm    
If you haven't tried anything so far, it's time to try. If you click on the article I just references, it can give you the idea why. And I'll add: it's also because nobody like to waste time. If you did not try anything, how can anyone be sure that some answer, no matter how good, could help you?
—SA
DarkDreamer08 18-Jan-15 19:12pm    
Thanks for that. I am trying now some thing that I found from the net but I got a problem. I will update my question :) Hope you still help me, Thanks in advance :)

1 solution

There's nothing wrong with your function that's returning an Enumerable<Tuple<string,int>>.

You just need to call the function with the 'Value property of the DateTimePicker Controls:

var months = MonthsBetween(dateTimePicker1.Value, dateTimePicker2.Value);

Here's an alternative way you could approach this using DateTime in a for-loop:
C#
private DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;

private List<Tuple<string, int>> BetweenMonths = new List<Tuple<string, int>>();

private void button1_Click(object sender, EventArgs e)
{
    DateTime startDate = dateTimePicker1.Value;
    DateTime endDate = dateTimePicker2.Value;

    if (endDate < startDate)
    {
        DateTime temp = endDate;
        endDate = startDate;
        startDate = temp;
    }

    BetweenMonths.Clear();

    for (DateTime date = startDate; date < endDate; date = date.AddMonths(1))
    {
        BetweenMonths.Add(Tuple.Create(dateTimeFormat.GetMonthName(date.Month), date.Year));
    }
}
 
Share this answer
 
v4
Comments
DarkDreamer08 18-Jan-15 20:08pm    
How am I supposed to display the months? when I insert it inside the message box, this appeared, "CompEnrollmentSystem.Sample2+<monthsbetween>d_0"
BillWoodruff 18-Jan-15 20:58pm    
"How am I supposed to display the months?"

Remember that your code returns an IEnumerable, and an IEnumerable is kind of an "intermediate" data-structure (some folks find it useful to think of an IEnumerable as a "map" to a Collection) that you can't use as a discrete object until you render it by converting it to a List, or Array, or enumerating it (for example, by using for-each on it).

So, if you wanted to display the results as the Items of a ListBox, you would need to do something like:

// using your code
listBox1.Items.AddRange(months.ToArray());

// using my code example
listBox1.Items.AddRange(BetweenMonths.ToArray());

The alternative method I showed here creates a List you can use right away; of course, it's a List of Tuples ... I used Tuples because that's what you used in your question.

Where you intent to display the results of the calculation may well dictate what form you choose for the data-structure you create.

If this doesn't help you, let me know. cheers, Bill
DarkDreamer08 18-Jan-15 21:03pm    
it helps me and it displays what I needed. I inserted a messagbox inside the for loop to call the date that I converted ToShortDateString. How am I supposed to render it in a list or array? :laugh: I am not good at it :(
DarkDreamer08 18-Jan-15 21:05pm    
Actually I make it as another question. I post your solution. Please help me there :( here is the link of it as another question: http://www.codeproject.com/Questions/867059/How-to-display-the-list-of-months-from-the-differe?arn=0
DarkDreamer08 19-Jan-15 1:05am    
Which part will I edit if I want also to add the end date that I input in my date timepicker? It was not included/added after I pressed the button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900