Friday, August 23, 2013

Adding the specified count to the semaphore would cause it to exceed its maximum count

I got this unusual exception while working on an ASP.NET project:
"Adding the specified count to the semaphore would cause it to exceed its maximum count error".

I thought it is something related to SQL Server Database Connection String.
After using Query Profiler I was able to run SQL queries directly without any problems.
So this means that the issue in ASP.NET Application not with the SQL Server Database.
I did a research about this issue to figure out a solution and I discovered that this issue related to ADO.NET Connection Pool manager.

The Connection Pool Manager is enabled by default in the connection string.
It will attempt to find a free connection, but if it didn't find a free connection it would threw the exception we are talking about.

To fix this issue all you have to do is to disable the Connection Pool Manager as shown below:
<add name="contest" connectionString="Data Source=Server1Test;Initial Catalog=DBTest;User ID=UserTest;
Password=PasswordTest;Pooling=False;" providerName="System.Data.SqlClient" />

Thursday, July 11, 2013

How to disable some context menu items on Telerik TreeView

There is an event named "OnClientContextMenuShowing" for Rad Treeview that calls javascipt function and passes 2 arguments "sender, args".
The Args parameter contains The current selected TreeNode and Context Menu Items.
You can enable or disable any Context menu item based on any attributes for the Current Selected Tree Node.

Here is a code snippet for the javascript function that demonstrate how can you get the current select Tree Node and the Context menu Items and how can you disable the items.
function onClientContextMenuShowing(sender, args) {
            var treeNode = args.get_node();
            if (treeNode._attributes != undefined) {
                if (treeNode._attributes._keys[0] == 'BoolFlag') {
                    // 0 refer to the Context Menu item index

Wednesday, May 1, 2013

How to Select from Data Table in C#

DataTable Object has a Select Method which acts like Where in SQL Server. So if you want to write filter query you write the column name directly without Where and the filter expression you want.

I made a simple example that creates a DataTable and fill it with some data and use Select method:

DataTable dt = new DataTable("MyDataTable");
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));

for (int i = 0; i < 10; i++)
    DataRow dr = dt.NewRow();
    dr["ID"] = i + 1;
    dr["Name"] = "Name " + (i + 1).ToString();


DataRow[] rows = dt.Select("ID = 3");

Friday, March 1, 2013

How to get Year, Month and Day out of SQL Date in SQL Server

Let is say that we have the following Date 2007-04-05 10:13:14.109 and we want to get Year, Month and Day out of this date.

There is a function named DATEPART() that can be used to archive this.
SET @MyDate = '2007-04-05 10:13:14.109'

The result would be:


Friday, February 1, 2013

How to know if a Date is in range between two Dates in C#

Let's say that we have a DateTime = Jan 1, 2012 and we want to check if this date is between 2 other dates.
The following code example Implements IRange interface that can be used with Integers too.
public interface IRange
        T Start { get; }
        T End { get; }
        bool WithInRange(T value);
        bool WithInRange(IRange range);

    public class DateRange : IRange
        public DateTime Start { get; set; }
        public DateTime End { get; set; }

        public DateRange(DateTime start, DateTime end)
            Start = start;
            End = end;

        public bool WithInRange(DateTime value)
            return (Start <= value) && (value <= End);

        public bool WithInRange(IRange range)
            return (Start <= range.Start) && (range.End <= End);

This is how to use this class function:
DateTime startDate = new DateTime(2011, 1, 1);
        DateTime endDate = new DateTime(2013, 1, 1);
        DateTime inRange = new DateTime(2012, 1, 1);
        DateTime outRange = new DateTime(2010, 1, 1);
        DateRange range = new DateRange(startDate, endDate);
        bool yes = range.WithInRange(inRange);
        bool no = range.WithInRange(outRange);

Tuesday, January 1, 2013

How to avoid Uncaught ReferenceError: $ is not defined in jQuery

Sometimes while developing an existing Web Application, I wanted to use jQuery function and I get this error "Uncaught ReferenceError: $ is not defined".

There are some reasons causing it and ways to avoid them:
  • Make sure the references to the jQuery scripts is loading first, but them in the head of master page or the page you are using.
  • Sometimes the references to the jQuery scripts is loading twice, make sure it loads only once.
  • If you have external js files with your function, which depend on other jQuery libraries, you have to load that library first, and then dependent JS file with your functions.

This will not work:
<script src="customlib.js"></script>
<script src=""></script>
But this will work:
<script src=""></script>
<script src="customlib.js"></script>