Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pass calendar control property value as argument into method

Posted on 2014-11-03
3
Medium Priority
?
252 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 35

Accepted Solution

by:
it_saige earned 2000 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 35

Expert Comment

by:it_saige
ID: 40421917
You are very welcome.

-saige-
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

824 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