Tuesday, May 13, 2014

Session Id changes in ASP.NET MVC on every new page request

I faced and issue in ASP.NET MVC application where the value of SessionID variable changes on consecutive page requests.

The reason is either you don't have a Global.asax file in your solution, or you have a global.asax in your solution without the definition of session end and session start. So the web server actually doesn't take care of session start and end event per application basis, and it generates a new key on every page request.
You should have a Global.asax in your project and it shall have following two functions added:

       protected void Session_Start()
        { }

        protected void Session_End()
        { }
    

This should work perfectly.......
Enjoy coding!

Monday, May 12, 2014

Generate a Pie Chart at runtime, C#, .NET

We can quickly write a function which returns a Pie Chart at runtime. Assuming that we are making a pie chart for the number of visitors/users from different countries, we will pass System.Data.DataTable as parameter to the function and it shall return a Chart object.

Please add following namespaces to the page/class.

using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing;
using System.Data;

Write the following function to get a Chart as return Parameter. You can pass a Datatable to the funtion with following schema,
Datatable dt (Country Text, Users Int)

Following is the function :

protected internal Chart GetPieChart(DataTable dtChartData)
        {
            Chart pieChart = new Chart()
            {
                Width = 800,
                Height = 800
            };

            ChartArea chartArea1 = new ChartArea();
            Legend legend1 = new Legend() { BackColor = Color.BurlyWood, ForeColor = Color.White, Title = "Countries" };
            chartArea1.Name = "PieChartArea";
            pieChart.ChartAreas.Add(chartArea1);
            pieChart.Dock = System.Windows.Forms.DockStyle.Fill;
            legend1.Name = "Legend1";
            pieChart.Legends.Add(legend1);
            pieChart.Location = new System.Drawing.Point(0, 100);

            pieChart.Series.Clear();
            pieChart.Palette = ChartColorPalette.Fire;
            pieChart.BackColor = Color.LightYellow;
            pieChart.Titles.Add("User & Country Analytics");
            pieChart.ChartAreas[0].BackColor = Color.Transparent;
            Series series1 = new Series
            {
                Name = "series1",
                IsVisibleInLegend = true,
                Color = System.Drawing.Color.Green,
                ChartType = SeriesChartType.Pie
            };
            pieChart.Series.Add(series1);

            int count = 0;
            foreach (DataRow dr in dtChartData.Rows)
            {
                series1.Points.Add(Convert.ToInt32(dr["Users"]));
                var p1 = series1.Points[count];
                p1.AxisLabel = Convert.ToInt32(dr["Users"]).ToString();
                p1.LegendText = Convert.ToString(dr["Country"]);
                count++;
            }
            pieChart.Invalidate();
            return pieChart;
        }

Hope this helps!

Sunday, May 11, 2014

Managing Crawl Rules in SharePoint 2013

It's very simple to configure crawling rules in SP 2013 as it was in 2010.
Lets have a quick walk through of it. Crawl rules are used to specfiy detailed logic to the crawler while content crawling. There are situations where we don't want to crawl a particular content or we want exceptional content to be crawled inside that. There are many option available.
  • Prevent content on a site from being crawled. For example, if you created a content source to crawl http://www.ks.com, but you do not want the search system to crawl content from the subdirectory http://www.ks.com/downloads, create a crawl rule to exclude content from that subdirectory.
  • Crawl content on a site that would be excluded otherwise. For example, if you excluded content from http://www.ks.com/downloads from being crawled, but you want content in the subdirectory http://www.ks.com/downloads/content to be crawled, create a crawl rule to include content from that subdirectory.
  • Specify authentication credentials. If a site to be crawled requires different credentials than those of the default content access account, create a crawl rule to specify the authentication credentials.
You can use wildcard(*) characters to include or exclude any content for crawl.Also, Crawl rules can be used to crawl content which is confidential or you may not want to crawl it or any oother account is required to crawl that with different credentials, you can specify all these in Crawl Rules.
How to create a new Crawl Rule:
  • Go to SharePoint Administration, make sure you are login with Search Admin Account.
  • Application Management section, click Manage Service Application.

  • On Manage Service application Page, click on Search Service Application. You will see the above Screen for Search Administration.
  • Click on Add New Crawl Rule.
  • Now as explained above you can specify what to include and what to exclude in this new crawl rule.
  • If you have selected " Include all items in this path" in Crawl Configuration, then only you can specify the authentication for this new Crawl Rule (in case if special authentication is required to implement the new rule).
Testing Crawl Rule
  1. On the Search Administration page, in the Crawling section, click Crawl Rules.
  2. On the Manage Crawl Rules page, in the Type a URL and click test to find out if it matches a rule box, type the URL that you want to test.
  3. Click Test. The result of the test appears below the Type a URL and click test to find out if it matches a rule box.
Happy SharePointing.......

Ref: MSDN Link

Visual Studio Build Error - The "GenerateResource" task failed unexpectedly

Error: "The GenerateResource task failed unexpectedly" This error is quite common while building the project in Visual Studio....