Wednesday, June 22, 2011

Binding Month days in DropDownList

public void BindDays()
{
for (int i = 1; i <= 31; i++)
{
ListItem item = new ListItem();
item.Value = i.ToString();
item.Text = i.ToString();
DropDownList1.Items.Add(item);
}
}

My virtual business card

Learn more about me !
https://www.mcpvirtualbusinesscard.com/VBCServer/passionate/profile

Monday, June 20, 2011

Calculating monthwise leaves in date range in C#.NET

Recently there was an requirement at my work was finding the leave month wise.
Assume this way,If an employee apply leaves for the duration between 4th Feb,2011 and
4th July 2011.HR team wanted to calculate the month wise leave for that employee against total number of leave days.
By doing this way there is a room to not to calculate the "Week end" and other government announced holidays.

Here is the code for to do so.

class Program
{


static void Main(string[] args)
{
MonthChecker cObj = new MonthChecker();
List listDate = new List();


List strList = new List();

DateTime from = new DateTime(2011, 02, 25);
DateTime to = new DateTime(2011, 04, 14);


DateTime dt2 = new DateTime(2011,02,25);
DateTime to2 = new DateTime(2011, 06, 14);

string n = "";
string n1 = "";

while (from <= to)
{
from = from.AddDays(1);
DateTime d = new DateTime();
d = from.AddDays(-1);
if (cObj.CheckMonthLastDay(d))
{

listDate.Add(d);
}

}

for(int i=0;i
{

if (i == 0)
{
TimeSpan span = listDate[i].Subtract(dt2);
n = n + "\n" + span.Days.ToString();
strList.Add(n);
}

else
{
DateTime ck = new DateTime(listDate[i].Year, listDate[i].Month, 01);
TimeSpan spanck = listDate[i].AddDays(1).Subtract(ck);
string M = spanck.Days.ToString();
strList.Add(M);
}

}

foreach (string str in strList)
{
Console.WriteLine(str);
}
Console.ReadLine();

}
}

Class MonthChecker
{
public DateTime GetLastDayOfMonth(DateTime dtDate)
{

int a = DateTime.DaysInMonth(dtDate.Year, dtDate.Month);
DateTime dt = new DateTime(dtDate.Year, dtDate.Month, a);
return dt;

}
public bool CheckMonthLastDay(DateTime dtDate)
{
DateTime d1 = dtDate;
if (d1 == GetLastDayOfMonth(dtDate))
{
return true;
}
else
{
return false;
}
}

}

Friday, June 17, 2011

Find Number of Days in specific month

Recently I encountered the situation to find the number of days in specific month of the given year.In ASP.NET 3.5 comes up with built-in method called "DaysInMonth".This method takes two parameter as Year and Month.
If you want to find out the number of days for "January",then pass the value 1 for Month parameter.
int numberOfDays = DateTime.DaysInMonth(Convert.ToInt32(YearList.SelectedItem.Value), Convert.ToInt32(MonthList.SelectedItem.Value))

retrieve unique rows from sharepoint List

To retrieve the unique rows from the sharepoint list use the "DefaultView" of the DataTable.
public DataTable GetLeaveAppliers()
        {
 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Leave Plan"];
                    SPQuery q = new SPQuery();
                    string query = "";
                    q.Query=query;
                    DataTable dt = list.GetItems(q).GetDataTable();
                    DataTable row= dt.DefaultView.ToTable(true, "Author");


                    return row;


                }

            }
}

Monday, June 06, 2011

ListItem Client OM

Listing all items in sharepoint list usingClient Object Model in SharePoint 2010(Client OM)
ClientContext cntx=new ClientContext("Site URL");
Web web = cntx.Current.Web;
List list=web.Lists.GetByTitle("List Name");
CamlQuery query=new CamlQuery();
query.ViewXML="";
ListItemCollection cols=list.GetItems(q);
cntx.Load(cols);
foreach(var item in cols)
{
list1.Items.Add(item["Title"]);
}
cntx.ExecuteQuery();

Friday, June 03, 2011

SharePoint 2010 Delegate Control

SharePoint 2010 Delegate Control

This control delegates asp.net control to render inside its template.
In your home page of the sharepoint site you can see the "MySite,Search Input box with lens icon and "Global Navigation".
These controls are called "Delegate Controls".You can create new asp.net web controls or user control and put
them inside the delegate template.Your customised user control will be render as per your sequence specified in
"Element.xml" of the sharepoint project solution.
If you want host multiple user controls in "Delegate template" then set the property of the Control "AllowMultipleControls" to true.
This will allow the delegate control consider as "Child control" as per the sequence.
The Lower sequence will always replace the highest sequence in rendering mode.
If you create the user control refer the controlSrc[usually it will be the virtual directory of ControlTemplate(_controltemplates) ]properties of "Control" in Element.xml
My Element.xml belows like.


  


on my UserControl's on Click of the Button events I just simply placed an javascript alert.
this.Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),"Greetings", "Type your script");
You can set scope on your "Feature File"