Solved

Pass calendar control property value as argument into method

Posted on 2014-11-03
3
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

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 34

Expert Comment

by:it_saige
ID: 40421917
You are very welcome.

-saige-
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

739 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