How to use Linq Lambda Expression IEqualityComparer For IEnumerable.Except

One of the thing that sometimes annoys is Lambda Expression with IEqualityComparer. I was trying to pick items with Except Comparer from the list. After doing quick search on google found the below extension method which can be used in such scenario. 

public static IEnumerable Except(this IEnumerable first, IEnumerable second, Func<TSource, TSource, bool> comparer)
{
return first.Where(x => second.Count(y => comparer(x, y)) == 0);
}

How to use:

List myItemsList = source.Except(myDeals, (x, y) => x.ID == y.ID).Take(requiredCount).ToList();

This will return the list of items except item(s) which match with an ID in myDeals. 

Share

Read More

Sitecore query with hypen or dashes

Challenge:

Sometimes your sitecore query might contains hypen or space and you get an error: “unterminated literal string in Query”. It is because the query contains an illegal character and the solution is to escape the “-“.So we need to add hash(#) around the word in the query.

Query: /sitecore/content/My Site/Event/ABC-Items

Earlier I referred the below article and implimented in one of the project:

Escaping dashes/“-” in Sitecore Queries. Datasource query Update

This is actually the nice example. But while working on another project, I applied below piece of code which I think is better and easy solution then the one given in above link.

Solution:


StringBuilder startItemPath = new StringBuilder(@"/");
startItemPath.Append(string.Join("/", myItemsFolder.Paths.FullPath.Split(new char[] { '/' },
StringSplitOptions.RemoveEmptyEntries).Select(x => string.Format("#{0}#", x))));

allItems = contextDb.SelectItems(“fast:” + startItemPath + “//*[@@templateid = ‘” + templateID + “‘]”).ToList();

By applying above solution. The query will contain “#” around all the words in query. It won’t create any harm for the query to execute.

If you know any other better approach then do comment below.

Happy Querying!

Share

Read More

Error: Could not load file or assembly Ninject

Challenge:

Error: Could not load file or assembly ‘Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7’ or one of its dependencies. The system cannot find the file specified.

Above error started appearing suddenly in our local Sitecore (Sitecore.NET 8.1 (rev. 151207)) project. We eyed lot of articles on-line but none helped, So that is why I thought it’s better to blog this out. We investigated for couple of hours to get this fixed, may be this post saves someone’s time.

(more…)

Share

Read More

Using AutoMapper

Challenge:

We defined the Web-Service and were using it in an application. Web-Service returns an object or list of an object.

 

There was already code written to do the processing once we get the response object from web-service.But when we get the object via web-service, it append the Web Service Reference Name because of which we were not able to utilize the already written code for processing that object. We believe in Re-usability as much as possible and wanted to re-utilize that code which was also used by other functions. Web-Service was returning lot of data which includes list and list of object inside the list etc.

Share

Read More

How to encode Trademark symbols ™ in C#

Hello Folks, Sorry was away for a while. As was very busy with Project.Challenge:
We were facing one strange issue with encoding Trademark symbol in C#. If you are also facing such similar issue then this post is for you.It was strange for us see that ™ isn’t encoding with HttpUtility.HttpEncode. As google is my best friend [so as yours], I tried to get the answer.

Before we dig into the solution, I tried on following websites which help us to get the result live for the Http.HtmlEncode()

http://htmlencode.org/
http://www.opinionatedgeek.com/dotnet/tools/htmlencode/encode.aspx

And we see that HTML Encode isn’t encoding ™.
(more…)

Share

Read More

How to escape line breaks while creating CSV or TSV file using C#

Hello C# friends.

Are you creating a CSV (Comma Separated Value) or TSV (Tab Separated Value) file in CSharp and you don’t know why your CSV file is breaking when you open it into Excel, then this post is for you.

Mr. Brian has written a very nice article on how to generate CSV file which we referred in our project.
http://www.csharptocsharp.com/generate-csv-from-generic-list

It was difficult to identify the problem. Values in object were html based and we were doing Html.Encode, so i thought that we need to do Html.Decode, but was unlucky.
To get better understanding of Html.Decode and Html.Encode refer: http://www.dotnetperls.com/httputility
Then thought that it can be  Encoding issue so tried to pass different encoding formats to StreamWriter but was unlucky in that too. I tried to identify in depth and reached to the problem and then solution.

Problem:
As my values were in Html format it were containing “t” or “n” or “r” which were creating an issue when writing a file using StreamWriter. As it get “t” or “n” or “r” it was breaking the file.

(more…)

Share

Read More

Migrate Visual Studio Solution from SVN to TFS

I love to work on Microsoft Technologies and while working with them recently we
got a challenge to migrate our project from SVN to TFS. So I would be
discussing about:
1) How to migrate visual studio solution from SVN to TFS.
2) Different approaches for migration
3) Challenges faced during migrating our solution.
As it is our basic approach to first always search on google and do some analysis
and research before doing any such task, similar way we did and found two
approaches – Manual and Automated.
SVN2TFS.EXE – (Automated) we didn’t dig much also read few
comments by which we got unhappy.

(more…)

Share

Read More

Setup SVN on Local Network

Subversioning
plays a very vital role for Developers. It keeps track of changes in a file over
time. It maintains the history of all the changes. This way you do not end up
with millions of copies of project that you cannot make heads or tails out of.  With a version control system, every time you
commit a set of changes you write a comment describing what you did. Then you
can look over the log to see what was changed when and by whom. You can easily
look at the differences between versions and easily roll back changes if you
need to. You also always have easy remote access to the current version of your
project without having to remember which file name you used for it: you simply
do an update of your working directory, or checkout the head version. For more
information regarding version control please refer: http://en.wikipedia.org/wiki/Revision_control
While working
on SVN, a question arrived in my mind that is there any way to setup SVN in
local network. Yes [Where there is will there is way] it’s possible. If you
have any project and your team is working on local network, then you can setup
svn on local network, it becomes easier for a team to work. It basically can be
very much useful for the collegians. Collegians must use this approach in all
projects. We can setup SVN on local machine connected in a network and that
will act as a SVN server for other team mates.

Prerequisites
Tortoise SVN (Subversion client) and
Ankh SVN (Subversion plugins for Visual Studio)

Note: we
already have created a visual studio solution.
Let’s have a
step by step look on how to setup SVN in local.

1)   
Create a folder where repository will
reside. I have created a folder name Repository Demo where my solution’s repository
will be saved when I check In.
2)   
Add project solution in Repository
Browse
the repository.
Our next step
is to add our solution to the SVN Repository we just created. We will add the
solution/project files under the trunk folder.
Add the
solution to repository directory.
You will see
that your solution is now added in repository once you add solution folder in
repository.
Provide the
network access rights for repository directory to be accessible by network
users. I am giving the Everyone access rights so now anyone having access to my
machine in network will be able to get the solution via this Repository.
3) Now we can
provide repository access to anyone in your network. Example: file://Nikki-PC/Repository
Demo/trunk
OR
Happy sub-versioning.
J
Share

Read More

Quick way to deploy web application to multiple environment.

Hello Folks. It’s
really interesting to know about MSBuild. Recently while working on one project
we found that deploying Project Files on multiple environment consumes lot of
time. So we thought to take the help of MSBuild and deploy project files on live
Server(s) automatically.
What is MSBuild?
MSBuild is Microsoft
Build Engine for Visual Studio. MSBuild is completely transparent with regards
to how to process and build software developed in Visual Studio.
Why MSBuild?

Initially from Visual
studio we do publish via Publish Profile(s). MSBuild uses that publish profile
and does deployment without the help of visual studio. It can reduce deployment
time up to 70%.

This is the basic Dev
environment which I have created to demonstrate you on Automatic Build.
Developer can publish files to staging as well production Environments.
How MSBuild?
MSBuild command needs
few command line options mainly MSBuild.exe, Visual Studio Project Solution
File (.sln), and Publish Profile.
First of all you need
to get the path of MSBuild.exe in my case it is located at C:WindowsMicrosoft.NETFramework64v4.0.30319msbuild.exe
Create a new File and enter
the below two MSBuild Commands. (Note: You need to change the path according to your project solution directory.)
“C:WindowsMicrosoft.NETFramework64v4.0.30319msbuild.exe”
“C:UsersnpunjabiDocumentsVisual Studio
2013ProjectsDeveloperEnvironmentDeveloperEnvironment.sln”
/p:DeployOnBuild=true /p:PublishProfile”C:UsersnpunjabiDocumentsVisual
Studio 2013ProjectsDeveloperEnvironment\DeveloperEnvironmentPropertiesPublishProfilesStaging.pubxml”
“C:WindowsMicrosoft.NETFramework64v4.0.30319msbuild.exe”
“C:UsersnpunjabiDocumentsVisual Studio
2013ProjectsDeveloperEnvironmentDeveloperEnvironment.sln”
/p:DeployOnBuild=true /p:PublishProfile”C:UsersnpunjabiDocumentsVisual
Studio 2013ProjectsDeveloperEnvironment\DeveloperEnvironmentPropertiesPublishProfilesProduction.pubxml”
/p:DeployOnBuild=true –
This will build the solution before deployment and will throw an error if build
fails before it deploys the files.
Save this file with
any name and provide extension as “.bat”.
Open Command Prompt
in Administrator Mode and execute the MSBuild (.bat) file.
Note: If you are using
SVN then before deployment you can also write the SVN Command which will update
the solution. After that MSBuild.exe will build the code and then publish.
Please download this file for
more user friendly deployment file which will prompt the user for deployment
whether to start deployment or not on particular servers. Thanks to Kiran Sir (http://kiranpatils.wordpress.com/) for helping me in creating deployment file.
To know more about Web Deploy : Click Here

If you have better
idea do share with us.

Share

Read More

Running Web Applications in IIS

Whether you built ASP.NET application or MVC application,
you can run your application in IIS.
Pre-requisites:
–         
Running IIS
–         
Visual Studio
If you don’t know how to enable IIS then, here is the link: http://bit.ly/1lAWh7H

If you already have a web application running in Visual
Studio Move to Step 2.
Step 1: Create Application

Create a Web Application ->
ASP.NET web application or MVC application.
(Note if you already have created your Application then go to Step 2.)


1) Add
new HTML Page and name it index

2) In
index.html page enter just few lines of code.
Below body tag write:

And
then save.
     Step 2: Go to IIS Manager (If you don’t know how to navigate to IIS then follow this link: http://bitly.com/1si7HC9)
– Under
Sites Folder -> Add Website

Enter
Site name, Select appropriate Physical path of website be careful in selecting
the project folder, Enter Host Name (Host name will be the URL with which you
will be able to access application).

Let’s Browse Website:

Now when you click Browse Website, you won’t be able to
browse it, because you haven’t made the host entry. System isn’t able to
identify that yes it has to look in local IIS for the application you want to
browse.
Step 3: Run Notepad application in Administrator Mode.
Open the hosts file which is located at
C:WindowsSystem32driversetc
Do the host entry –
127.0.0.1      aspapplication.com
Now Let’s Browse.
You will come up on following error:

This is because IIS do not have read permissions.
Step 4: Provide permissions to IIS
For giving permissions to IIS right click on your Website in
IIS and then go to Edit Permissions. Then
go to Security Tab, click on Edit, click on Add, click on Advanced, click on
Find Now, and then select IUSR OK,
Again click on Advanced select IIS_IUSRS and then OK, OK, OK.

Provide full control to both groups.
That’s it.!
You can now browse your application with URL:
aspapplication.com
Feel free to fire queries.
Happy Coding..!!! J
J
Share

Read More