Solved

Pass calendar control property value as argument into method

Posted on 2014-11-03
3
236 Views
Last Modified: 2014-11-04
Hi, how can I set a variable for a control property?  Ie, for a Calendar control VisibleDate property I would like to pass the value into the  GetFirstDayOfNextMonth() method shown below.  So if I have multiple calendar controls I would like to pass say (pseudo)
if
(Calendar_Jan.VisibleDate.Month = 12 / Calendar_Feb.VisibleDate.Month = 12 / Calendar_Month.VisibleDate.Month = 12) == 12
{

}

so I want to pass Calendar_XXX.VisibleDate.Month as an argument into GetFirstDayOfNextMonth()

protected DataSet dsAgent;

protected DateTime GetFirstDayOfNextMonth()
        {
            int monthNumber, yearNumber;
            if (Calendar_Jan.VisibleDate.Month == 12)
            {
                monthNumber = 1;
                yearNumber = Calendar_Jan.VisibleDate.Year + 1;
            }
            else
            {
                monthNumber = Calendar_Jan.VisibleDate.Month + 1;
                yearNumber = Calendar_Jan.VisibleDate.Year;
            }
            DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
            return lastDate;
        }

        protected void Fill_ds()
        {
            DateTime firstDate = new DateTime(Calendar_Jan.VisibleDate.Year, cld.VisibleDate.Month, 1);
            DateTime lastDate = GetFirstDayOfNextMonth();           
            Int32 empNum = 6022022;
            dsAgent = GetAgent(firstDate, lastDate,empNum);

        }

        protected DataSet GetAgent(DateTime startDate, DateTime endDate, Int32 empNum)
        {
            String qryAgent = "select * from dbo.tbl_Table where c_id = @empNum and DTE between @startDate and @endDate";
            DataSet dsGetAgent = new DataSet();

            using (SqlConnection conn = new SqlConnection(strConn))
            {
                using (SqlCommand cmd = new SqlCommand(qryAgent, conn))
                {
                    cmd.Parameters.Add(new SqlParameter("@startDate", @startDate));
                    cmd.Parameters.Add(new SqlParameter("@endDate", @endDate));
                    cmd.Parameters.Add(new SqlParameter("@empPein", empNum));
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    try
                    {
                        da.Fill(dsGetAgent);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    return dsGetAgent;
                }

            }
        }

Open in new window

0
Comment
Question by:Scarlett72
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40420726
I dont know what Type your calendar control is, but this is one way to do it:
protected DataSet dsAgent;

protected DateTime GetFirstDayOfNextMonth(Calendar calendar)
{
	int monthNumber, yearNumber;
	if (calendar.VisibleDate.Month == 12)
	{
		monthNumber = 1;
		yearNumber = calendar.VisibleDate.Year + 1;
	}
	else
	{
		monthNumber = calendar.VisibleDate.Month + 1;
		yearNumber = calendar.VisibleDate.Year;
	}
	DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
	return lastDate;
}

protected void Fill_ds()
{
	DateTime firstDate = new DateTime(Calendar_Jan.VisibleDate.Year, cld.VisibleDate.Month, 1);
	DateTime lastDate = GetFirstDayOfNextMonth();           
	Int32 empNum = 6022022;
	dsAgent = GetAgent(firstDate, lastDate,empNum);
}

protected DataSet GetAgent(DateTime startDate, DateTime endDate, Int32 empNum)
{
	String qryAgent = "select * from dbo.tbl_Table where c_id = @empNum and DTE between @startDate and @endDate";
	DataSet dsGetAgent = new DataSet();

	using (SqlConnection conn = new SqlConnection(strConn))
	{
		using (SqlCommand cmd = new SqlCommand(qryAgent, conn))
		{
			cmd.Parameters.Add(new SqlParameter("@startDate", @startDate));
			cmd.Parameters.Add(new SqlParameter("@endDate", @endDate));
			cmd.Parameters.Add(new SqlParameter("@empPein", empNum));
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			try
			{
				da.Fill(dsGetAgent);
			}
			catch (Exception)
			{
				throw;
			}
			return dsGetAgent;
		}
	}
}

Open in new window


Ofcouse, I would prefer to use an extension method:
public static class Extensions
{
	protected static DateTime GetFirstDayOfNextMonth<T>(this T calendar) where T : Calendar
	{
		int monthNumber, yearNumber;
		if (calendar.VisibleDate.Month == 12)
		{
			monthNumber = 1;
			yearNumber = calendar.VisibleDate.Year + 1;
		}
		else
		{
			monthNumber = calendar.VisibleDate.Month + 1;
			yearNumber = calendar.VisibleDate.Year;
		}
		return new DateTime(yearNumber, monthNumber, 1);
	}
}

Open in new window


Using the extension you would simply have to call it from your control itself:
DateTime result = Calendar_Jan.GetFirstDayOfNextMonth();
// or you could call it like this as well
DateTime result2 = Extensions.GetFirstDayOfNextMonth(Calendar_Jan);

Open in new window


-saige-
0
 

Author Closing Comment

by:Scarlett72
ID: 40421886
Thank you -saige- this answered my question nicely, and has introduced the concept of 'Extension methods' to me; which I have to be honest is advanced for my skill level, never the less has taken me down a new rabbit hole  : )
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40421917
You are very welcome.

-saige-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
consuming an asmx web service in winforms application 3 24
Server Error 11 47
How would you add MULTITHREADING to the attached C# code? 4 49
Error in script 11 44
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A short film showing how OnPage and Connectwise integration works.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now