Wednesday, January 29, 2014

Issue "Unable to launch the IIS Express Web server"

Today I was figuring out the issue “Unable to launch the IIS Express Web server, Port is in use”
So, I was pushing hard to find the way how to manually assign the port to my VS Solution.
Here's the one:

Steps to manually assign the port to VS 2012 Solution:
1. Right Click on the Start-up executable Project that you are trying to run and select option properties. (See Screenshot 1 below)

 Screenshot 1


2. Now, You will the list of options in the left-panel menu. Select option 'Web'. (See Screenshot 2)




Screenshot 2

3. Here you will find the option 'Use Visual Studio Developement Server' under the Servers Section
4. Do Check it, select Specific Port and assign your own port number (say 8115 See Screenshot 2 above). Let the other options remain intact.
5. I hope you will able to run your project successfully.

Thanks
Amit Jain

Wednesday, January 22, 2014

Manage NuGet packages when moving the solution from one location to another

I was stuck in one issue today when I got one new solution to work on, i.e. some compilation issues in nuget packages such as angularJS, webgrease, optimization etc.

So, to avoid resintalling all packages again and again from nuget, you need to use the following way in Visual Studio.

You need to enable nuget package restore on Visual Studio solution. To enable it
1. Right Click the Solution
2. Select 'Enable Nuget Package Restore'.
3. Build the solution.


I hope this will save your time in such issues.


Thanks..Happy Coding :)

General Steps to install / uninstall the .Net Windows Service

How to Install the Service:
1. Go to Command Prompt and run as administrator option
2. type Command 'C:\Windows\Microsoft.NET\Framework\v4.0.30319' as by default installutil.exe is stored at this path.
3. Now Copy the fullpath where compiled service is stored (e.g. my system's local path is D:/TestService/TestService.exe)
4. Run the following command:
installutil.exe /i D:/TestService/TestService.exe
5. By doing the 4th step, your service will be installed.
6. Now, Go to Run -> type 'services.msc' to open the running window services.
7. Search for TestService installed, start the service

How uninstall the window service:
1. Repeat the above steps 1 to 3
2. Run the following command:
installutil.exe /u D:/TestService/TestService.exe

Please reply / comment if miss anything above.

Monday, January 13, 2014

Return Json Data to Ajax Success call in asp.net

In asp.net, there are many ways to return Json data to your Ajax Success Method. One of them is as follows:

Create a static class as ExtensionClass:

using System.Web.Script.Serialization;
namespace Extensions
{
        public static string ToJson(this object obj)
        {
            var serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
        }

        public static string ToJson(this object obj, int recursionDepth)
        {
            var serializer = new JavaScriptSerializer {RecursionLimit = recursionDepth};
            return serializer.Serialize(obj);
        }
}

Make sure you use the namespace System.Web.Script.Serialization to use the JavaScriptSerializer() as above.

Now, to make use of the above statement in return web method. Use the following:

Class having properties:

public class JsonData
{
       public string Id{get;set;}
       public string Name{get;set;}
       public string Status{get;set;}
}

Test.aspx.cs Web Page:
[System.Web.Services.WebMethod]
public static string ShowInfo()
{
     //Your Set of Statements
    var jsonData = new JsonData
                                               {
                                                   Id = "1",
                                                   Name = "Amit Jain",
                                                   Status = "Ok"
                                               };
    return jsonData.ToJson();
}


Jquery Ajax Call:
<script type='text/javascript'>
function testMethod()
{
     $.ajax({
                    type: "POST",
                    contentType: "application/json",
                    url: "Test.aspx/ShowInfo",
                    dataType: "json",
                    data: "",
                    success:
                    function (Result) {                              //Success Method
                        if (Result != '') {
                            var jSonData = JSON.parse(Result.d);
                             alert(jSonData.Name);
                        }
                    },
                    error:
                    function (XMLHttpRequest, textStatus, errorThrown) {
                    }
                });
}
</script>


I hope this helps.

Thanks
Amit Jain

Thursday, January 2, 2014

Set the First Day of the week in Sql

By Default, the first day of the week is set as Sunday. Now to change it to Monday. Enter the following command before your Sql Query:

Set DateFirst 1

Now It sets the first day of the week as Monday.

Cheers !

Friday, December 27, 2013

Jquery Ajax Call in Asp.Net

 <div id="Result">Click here</div>
<script type="text/javascript">
    $('#Result').click(function() {
      $.ajax({
        type: "POST",
        url: "Default.aspx/HelloWorld",
        data: "{}",
        contentType: "application/json",
        dataType: "json",
        success: function(msg) {
          // Replace the div's content with the page method's return.
          $("#Result").text(msg.d);
        }
      });
    });
  </script>

Phone Number Validation Class using IDictionary

Class:
public class PhoneValidator {
static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>() {
{ "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
{ "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)")},
{ "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},
};
public static bool IsValidNumber(string phoneNumber, string country) {
if (country != null && countryRegex.ContainsKey(country))
return countryRegex[country].IsMatch(phoneNumber);
else
return false;
}
public static IEnumerable<string> Countries {
get {
return countryRegex.Keys;
}
}
}
Calling:
if (!PhoneValidator.IsValidNumber("+1-1234-232-232", "USA"))
      Console.Writeline('Enter Valid Phone Number');