Thursday, December 12, 2013

SqlDependency must be called for each server that is being executed

Recently we rewrote our web applications in VS 2012 and .Net 4.0 and hosted it on IIS 7.For caching content I used the Sql Dependency and SQL service Service Broker.On test environment with load balancing we had two servers configured and everything was working as expected.


However when I moved the applications to production I started getting this error.







The only difference was the connectionstring which specified a failover server.

<add name="CONTENTDSN" connectionString="Data Source=Server1;Failover Partner=Server2;Initial Catalog=Database_Name;uid=UserId;pwd=password;"/>

The error was due to the failover server not having the database objects.Now when you use SQL dependency make sure that the failover server i.e Server2, has all the necessary database objects as the main server i.e. Server1.

Tuesday, November 5, 2013

asp.net - Could not load type

Today while I was converting a Web Application to a WebSite in Visual Studio 2012 I came across this error.I created a new website and starting adding the pages from the web application.Once I was done adding the pages, I did a build on the website and I encountered the "Could not load type ' '" error in my website.

After searching for an answer I found this error can occur for various reasons, in my case though it was because of the CodeBehind attribute of the aspx pages.When you add a aspx page to a web application the CodeBehind attribute is set.

<%@ Page Title="About Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="About" %>

If you import this page into a website you will come across Could not load type About error.The fix was to change the attribute to CodeFile in the website aspx page

<%@ Page Title="About Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %>

See here for CodeFile vs CodeBehind
http://stackoverflow.com/questions/73022/codefile-vs-codebehind





















Friday, November 1, 2013

c# - List strings in order of occurrence in a given string

In the project that I am working on right now I had to figure out a way to list a given set of strings in another string based on the order in which the list of strings would occur.

For example: I have a few raw html page content extracted from the web and I have a list of words.The task is to build a grid display that would list those words in order of occurrence in the content of a page. 

Below is the sample code using a console to achieve it.If there is a better way to do this, I am interested to know.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringOrder

{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Set of random words to search for in Content
                string[] saArrayofStrings = { "abc", "def", "mno", "xyz", "jkl" };
                //Content
                string sString = "xyz The history of the United States as covered in American schools and universities typically begins with either Christopher Columbus's 1492 voyage to the Americas or with the prehistory of the Native peoples, with the latter approach having become increasingly common in recent decades.[1]jkl Indigenous peoples lived in what is now the United States for thousands of years and developed complex cultures before European colonists began to arrive, mostly from England, after 1600. The Spanish had early settlements in Florida and the Southwest, and the French along the Mississippi River and Gulf Coast.abc By the 1770s, thirteen British colonies contained two and a half million people along the Atlantic coast, east of the Appalachian Mountains.mno The colonies were prosperous and growing rapidly, and had developed their own autonomous political and legal systems. However, with the end of the French and Indian War in 1763, Great Britain altered its relationships with the colonies by imposing tighter administrative controls and greater financial obligations on the colonists [2]. Tensions grew, eventually leading to armed conflict beginning in April 1775. def On July 4, 1776, the colonies declared independence from the Kingdom of Great Britain.";
                SortedList<int, string> OrderList = new SortedList<int, string>();
                foreach (String s in saArrayofStrings)
                {
                    if (sString.Contains(s))
                        OrderList.Add(sString.IndexOf(s), s);
                }
                Console.WriteLine("The order of occurrence of the given list of words in the content is below");
                foreach (KeyValuePair<int, string> pair in OrderList)
                    Console.WriteLine(pair.Value + " occurs at the following position:" + pair.Key.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
}

Output:





Friday, October 4, 2013

Visual Studio 2012 - The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)

In Visual Studio 2012 if you encounter this error when you add a new class to the solution,check to see the build action of the class in the properties of the class.
By default the build action is set to 'content'.Change it to 'compile'.See below





Thursday, August 8, 2013

vs 2012 - The primary reference could not be resolved because it has an indirect dependency on the framework assembly

I started getting this error after changing the target framework in build configuration for my web application in Visual Studio 2012.












To resolve this I had to unload the project and reload it into the solution.Right Click on the application in VS 2012 solution and select Unload Project in the context menu.

























Reload the project by right clicking on the project that is not available



asp.net - Method not found: 'Void System.Web.UI.ScriptResourceDefinition.set_LoadSuccessExpression(System.String)'.

You will get this error if you try to host your application on IIS Application pool running on .Net. 4.0 and your web application is built in .Net 4.5.By default the selected target framework for Visual Studio 2012 is .Net 4.5.

Right Click on your application in Visual Studio 2012, select the build configuration and choose .Net 4.0 framework.



Wednesday, August 7, 2013

asp.net - System.BadImageFormatException: Could not load file or assembly '' or one of its dependencies. An attempt was made to load a program with an incorrect format

You will get this error if you try to host a 32 bit web application on IIS7.To fix this error set "Enable 32 bit applications" flag to true in AppPool Advance Settings.




Wednesday, July 31, 2013

vs 2012 - The following module was built either with optimizations enabled or without debug information

I started getting this warning window while debugging one of my projects and to workaround it I had to change my debugger settings.






Thursday, July 18, 2013

asp.net - Access Master Page Label from Content Page

The below screenshots show how one can have a label on master page for displaying messages to the user using the application.

Site.Master 














Site.Master.cs 















Default.aspx 
Default.aspx.cs


Friday, June 21, 2013

Saving changes not permitted

If you try to use the SQL Server designer for making changes to the table you just created, then you will get a dialog box that tells you that you cannot save the changes.One work around this issue is to disable the option "Prevent saving changes that require the table to be re-created".

To disable this option see the screenshots below.
































































HTH