How To Open Explorer To A Specific Directory Using C#



TODO:

Have you ever wanted to launch explorer from your C# application to a specified directory?

 

SOLUTION:

using System.Diagnostics;

//class and other code goes here...

//set your location here
string explorerLocation = "C:\\Temp\\MyLocation";

//open the location
System.Diagnostics.Process.Start(explorerLocation);

 

NOTES:

There are no notes on this topic

How To Get The Text Value Rather Than The HTML Value Of A MultiLine ListItem Using SharePoint Client Object Model



TODO:

You have a multi-line field in a list.  When you get the value, it comes back as HTML when you reference it as item["field"].  To get the text, rather than the HTML, you need to do the following.

 

SOLUTION:

//put at top!!!
using SP = Microsoft.SharePoint.Client;
 
//put inside class !!! 
SP.ListItem targetItem = null;
//todo, set your targetItem = to the item you want.
 
using(ClientContext clientContext = new ClientContext("http://your.url.goes.here"))
{
     var itemFieldValues = targetItem.FieldValuesAsText;
     clientContext.Load(itemFieldValues);
     clientContext.ExecuteQuery();
}
string fieldTextValue = itemFieldValues["My item Title"];

 

NOTES:

There are no notes on this topic.

How To Use Excel.Application.GetSaveAsFilename() Method



TODO:

Have you ever wanted to use the Excel.Application.GetSaveAsFilename method to save your files?


SOLUTION:

Excel.Application excel = new Excel.Application();

FileInfo fileInfo = new FileInfo(txtInputFile.Text.Trim());

//so show the save as, and then filter by current extension only....reason is, that SaveAs() will save in current format, so format changes cannot happen here.
object fileName = excel.GetSaveAsFilename(fileInfo.Name, string.Format("Excel files (*{0}), *{0}", fileInfo.Extension), 1, "Save File Location");

 

NOTES:

This example will display the Save As dialog box.  I limit the box to the current file type, as calling excel.SaveAs(filename) will not change the file type.

How To Fix A Missing Local Publishing Profile Visual Studio 2012



TODO:

Have you lost the ability to publish your website locally?  If so, then you lost your profile for local publishing.

 

SOLUTION:

The second post on this link, has a correct solution.  Basically you need to add a new profile, choose File System as the type, and select a valid location.

 

NOTES:

There are no notes on this topic.

How To Add A Custom Pre-Requisite To A Click-Once Application



TODO:

Have you ever wanted to add your own prerequisites to a Click-Once application?

 

SOLUTION:

1.  Follow the instructions here to create a package manifest.

2.  Follow the instructions here to create a product manifest.

3.  Last restart Visual Studio and you will see your prerequisites in the list.

 

NOTES:

There are no notes on this topic.

How To Truncate a Decimal To X Places



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

How To Fix "Unable to update the entityset because it has a defining query and no UpdateFunction element exists in the ModificationFunctionMapping element to support the current operation" Error



TODO:

You try to insert a record using the Entity Framework and get the following error:

"Unable to update the entityset because it has a defining query and no <UpdateFunction> element exists in the <ModificationFunctionMapping> element to support the current operation"

 

SOLUTION:

Add a primary key to the table.  Also make sure the Concurrency Mode of the Primary Key is set to "Fixed" (right click column, choose properties)

 

NOTES:

There are no notes on this topic.

How To Insert A Record Using The Entity Framework And Get Back The Identity Of the Record



TODO:

Have you ever wanted to use the Entity Framework to insert a new record into the database, and get back the identity associated with it?

 

SOLUTION:

using(DataEntities dataEntities = new DataEntities())
{
       FormNumber formNumber = new FormNumber();
       dataEntities.AddToFormNumbers(formNumber);
       dataEntities.SaveChanges();
       return formNumber.Id;
}

 

NOTES:

This assumes you have an EDMX created called "DataEntities".  It assumes you have a table called "FormNumber", with an Identity column called "Id".

How To Find A String And Replace It With Another String Using C#



TODO:

Have you ever wanted to replace a string that exists in another string?

 

SOLUTION:

string foo = "123456-7890-abcd";
string bar = foo.Replace("-", "");

 

NOTES:

The example above will replace '-' with an empty string.  The result will be 1234567890abcd.

How To Remove The Last X Characters From A String Using C#



TODO:

Have you ever wanted to remove the last X characters from a string in C#?

 

SOLUTION:

string foo = "1234567890abcd";
string bar = foo.Substring(foo.Length - 4);

 

NOTES:

The example above will remove the last 4 characters from foo, and store the result in bar.