TODO:
Have you ever wanted to truncate a decimal to X places?
SOLUTION:
decimal rate = .07654;
decimal newRate = Math.Truncate(rate * 1000m) / 1000m;
NOTES:
The result will be .076
TODO:
Have you ever wanted to take a List<string> and convert it into a delimited string?
SOLUTION:
string someString = "test1.txt|test2.txt|test3.txt;
//split the string on | to get a List<string>
List<string> files = someString.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
//now flatten it to a delimited string
string newValue = string.Join("|", files.Select(x => x).ToArray());
NOTES:
There are no notes on this topic
TODO:
Have you ever wanted to get the current user that is logged into SharePoint?
SOLUTION:
//set textfield to the user
txtModificationBy.Text = this.Web.CurrentUser.LoginName;
NOTES:
There are no notes on this topic.
TODO:
Have you ever wanted to insert the contents of a DataTable efficiently using SqlClient.BulkCopy?
SOLUTION:
public static void FillMonteCarloSimulationResults(DataTable InDataTable)
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy("myconnectionstring")
{
//***NOW Do column mappings. This maps columns from DataTable (left) to DB Table (right)
bulkCopy.DestinationTableName = "Results";
bulkCopy.ColumnMappings.Add("EmployeeId", "EmployeeId");
bulkCopy.ColumnMappings.Add("Name", "Name");
bulkCopy.WriteToServer(InDataTable);
}
}
NOTES:
This example will take the DataTable, map the columns, then preform BulkCopy
TODO:
Have you ever wanted to change the title of your Application Page dynamically?
SOLUTIONS:
protected void SetPageTitles(string Title)
{
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderPageTitle");
contentPlaceHolder.Controls.Clear();
LiteralControl title = new LiteralControl();
title.Text = Title;
contentPlaceHolder.Controls.Add(title);
contentPlaceHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderPageTitleInTitleArea");
contentPlaceHolder.Controls.Clear();
title = new LiteralControl();
title.Text = Title;
contentPlaceHolder.Controls.Add(title);
}
NOTES:
There are no notes on this topic.
TODO:
Have you ever wanted to secure an Sharepoint Application Page by User Group? If so, just use the method below, and call it in Page_Load.
SOLUTION:
/// <summary>
/// Validate that the page can be accessed
/// </summary>
/// <returns></returns>
protected void ValidatePageAccess(string[] AllowedUserGroups)
{
try
{
//get web object
using (SPWeb currentWeb = this.Web)
{
#region Allowed Groups
SPGroup allowedGroup = null;
bool userHasAccess = false;
//check each, if one group is OK, all are.
foreach (string group in AllowedUserGroups)
{
allowedGroup = currentWeb.Groups[group];
//see if user is in allowed group
if (allowedGroup.ContainsCurrentUser)
{
if (Request.HttpMethod == "POST")
{
SPUtility.ValidateFormDigest();
}
userHasAccess = true;
break;
}
}
//now check access
if(!userHasAccess)
{
throw new Exception("User does not have access");
}
#endregion
}
}
catch (Exception x)
{
Response.Redirect("/_layouts/accessdenied.aspx");
}
}
NOTES:
There are no notes on this topic.
TODO:
You have a list of objects, and you want to update all items with a particular value, based off of a particular condition.
SOLUTION:
string newAccountNumber ="12345";
string previousAccountNumber = "54321";
foreach (var account in dsAccountList.FindAll(a => a.AccountNumber == previousAccountNumber))
{
account.AccountNumber = newAccountNumber;
}
NOTES:
The example will update the account number for all accounts with AccountNumber = previousAccountNumber
TODO:
You have a list of objects (List<T>), let's just say List<Dog> for this example. You want to sort them by a property called "Breed". This example will show you just how to do that.
SOLUTION:
List myDogList = myDogList.OrderBy(d => d.Breed).ToList();
//or
List myDogList = myDogList.OrderByDescending(d => d.Breed).ToList();
NOTES:
Your list of dogs will now be sorted by Breed.
*****Note that the default sort order is ascending.
TODO:
You open a website in Visual Studio, and get the dreaded "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS" error message.
SOLUTION:
1. Make sure you opened the site and not the parent folder.
2. In my case, I created a folder called "backup", and copied my entire site to it. Therefore, when I opened the site, that folder was loaded, and it too had a Web.Config file in it (since this folder contained a complete site backup). Simple remove that Web.Config and you will be set.
example showing my problematic folder structure:
root
Web.Config
site folder 1
site folder 2
backup
Web.Config
site folder 1
site folder 2
NOTES:
The presence of the backup folder in red is the culprit. It contained a backup copy of the root web.config, which contained entries only allowed in the root level Web.Config.
TODO:
Have you ever wanted to recursively clear a screen, without the need to manually clear each control? This can be extremely helpful when it comes to large forms.
SOLUTION:
public void ClearControls(Control InControl, bool ClearLists, bool SetRadioButtonDefaultToFirstItem, bool SetDropDownDefaultToFirstItem)
{
foreach (Control control in InControl.Controls)
{
//if its a check box, just clear the text
if (control is CftcTextBox)
{
((CftcTextBox)control).Text = string.Empty;
}
else if (control is CheckBoxList)
{
//deselect each check box item.
foreach (System.Web.UI.WebControls.ListItem item in ((CheckBoxList)control).Items)
{
item.Selected = false;
}
}
else if (control is DropDownList)
{
//deselect all items
foreach (System.Web.UI.WebControls.ListItem item in ((DropDownList)control).Items)
{
item.Selected = false;
}
//if we choose, we now select the first item in list
if (SetDropDownDefaultToFirstItem)
{
if(((DropDownList)control).Items.Count > 0)
{
((DropDownList)control).SelectedIndex = 0;
}
}
}
else if (control is System.Web.UI.WebControls.ListBox)
{
//if we clear lists, then remove all items. otherwise, just deselect all list items.
if (ClearLists)
{
((System.Web.UI.WebControls.ListBox)control).Items.Clear();
}
else
{
foreach (System.Web.UI.WebControls.ListItem item in ((System.Web.UI.WebControls.ListBox)control).Items)
{
item.Selected = false;
}
}
}
else if (control is RadioButtonList)
{
//default to the first item, if we choose to
if (SetRadioButtonDefaultToFirstItem)
{
if (((RadioButtonList)control).Items.Count > 0)
{
((RadioButtonList)control).Items[0].Selected = true;
}
}
}
//now call recursively
if (control.HasControls())
ClearControls(control, ClearLists, SetRadioButtonDefaultToFirstItem, SetDropDownDefaultToFirstItem);
}
}
NOTES:
There are no notes on this topic.