Solved

Pass calendar control property value as argument into method

Posted on 2014-11-03
3
233 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 32

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 32

Expert Comment

by:it_saige
ID: 40421917
You are very welcome.

-saige-
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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