Thursday, 25 February 2016

How to debug the Web API in ASP.NET MVC?


Many  developers are having question of how to debug the Web API from client project or from source that consumes Web API.

 For debugging the Web API there's no need to use any new extension, well you can do this stuff in Visual Studio itself.


  1. Go to  Solution Explorer where you would be having webapi project and the project that consumes web api.
  2. Now right click and select properties or hit Alt+Enter.
  3. Now in Startup Project select multiple  startup project.
  4. Go to Action and select Start in dropdown for both projects.
  5. That's it now click Apply and Ok. 
  6. Now create a breakpoint in both projects and enjoy the debugging.


Monday, 22 February 2016

All Basic examples of C# Programming| Fibonacci Series| Armstrong Number| Swap Numbers| Reverse String| Floyd's Triangle | Pascal Triangle

This may help any beginner, Mid-Level & might some times Experts too. 

This snippet includes:
  1. Odd-Even
  2. Check Vowel
  3. Count Vowels and Consonants
  4. Leap Year
  5. Factorial
  6. HCF & LCM
  7. Decimal to Binary
  8. Summation of n numbers
  9. Swapping with & without 3rd Variable
  10. Reverse
  11. Check Armstrong
  12. Fibonacci Series
  13. Remove Blank Space
  14. Floyd's Triangle
  15. Pascal's Triangle
using System;    
using System.Collections;    
using System.Collections.Generic;    
using System.Linq;    
using System.Text;    
using System.Threading.Tasks;    
    
    
namespace AllBasic    
{    
    class Program    
    {    
        static void Main(string[] args)    
        {    
        Restart:    
            Console.WriteLine("Enter your query \n");    
            string read = Console.ReadLine();    
    
    
            if (read == "Odd-Even")    
            {    
                int range1 = Convert.ToInt32(Console.ReadLine());    
                int range2 = Convert.ToInt32(Console.ReadLine());    
    
                for (int i = range1; i <= range2; i++)    
                {    
                    if (i % 2 == 0)    
                    {    
                        Console.WriteLine(i + " number is Even.\n");    
    
                    }    
                    else    
                    {    
                        Console.WriteLine(i + " number is Odd.\n");    
    
                    }    
                }    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Check Vowel")    
            {    
                string[] vowels = { "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" };    
    
                string getinput = Console.ReadLine();    
    
                if (vowels.Contains(getinput))    
                {    
                    Console.WriteLine("Yes!" + getinput + " is a vowel.");    
    
                }    
                else    
                {    
                    Console.WriteLine("No!" + getinput + " is not a vowel.");    
                }    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Count Vowels & Consonants")    
            {    
                string[] vowels = { "a", "e", "i", "o", "u", "A", "E", "I", "O", "U" };    
    
                string getinput = Console.ReadLine();    
                int vowelscnt = 0;    
                int consonantscnt = 0;    
                foreach (char character in getinput)    
                {    
                    if (vowels.Contains(character.ToString()))    
                    {    
                        Console.WriteLine("Yes!" + character + " is a vowel.");    
                        vowelscnt++;    
                    }    
                    else    
                    {    
                        Console.WriteLine("No!" + character + " is not a vowel.");    
                        consonantscnt++;    
                    }    
                }    
                Console.WriteLine("Total Vowels:" + vowelscnt + " and Consonants are:" + consonantscnt);    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Leap Year")    
            {    
                int getinput = Convert.ToInt32(Console.ReadLine());    
                if (getinput % 4 == 0)    
                {    
                    Console.WriteLine("Yes!" + getinput + " is a leap year.");    
                }    
                else    
                {    
                    Console.WriteLine("No!" + getinput + " is not a leap year.");    
                }    
                Console.ReadLine();    
            }    
            else if (read == "Factorial")    
            {    
                int getinput = Convert.ToInt32(Console.ReadLine());    
                int fact = 1;    
                for (int i = 1; i <= getinput; i++)    
                {    
                    fact = fact * i;    
                }    
                Console.WriteLine("Factorial of " + getinput + "! is:" + fact);    
    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "HCF & LCM")    
            {    
                Console.WriteLine("Enter two numbers:");    
    
                int getinput1 = Convert.ToInt32(Console.ReadLine());    
                int getinput2 = Convert.ToInt32(Console.ReadLine());    
                int temp;    
                int hcf; //  or gcd    
                int lcm, x, y;    
    
                x = getinput1;    
                y = getinput2;    
    
                while (getinput2 != 0)    
                {    
                    temp = getinput2;    
                    getinput2 = getinput1 % getinput2;    
                    getinput1 = temp;    
                }    
    
                hcf = getinput1;    
                lcm = (x * y) / hcf;    
    
    
                Console.WriteLine("Greatest Common Divisor of " + x + " & " + y + " is:" + hcf);    
                Console.WriteLine("Least Common Multiple of " + x + " & " + y + " is:" + lcm);    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "HCF & LCM")    
            {    
                Console.WriteLine("Enter two numbers:");    
    
                int getinput1 = Convert.ToInt32(Console.ReadLine());    
                int getinput2 = Convert.ToInt32(Console.ReadLine());    
                int temp;    
                int hcf; //  or gcd    
                int lcm, x, y;    
    
                x = getinput1;    
                y = getinput2;    
    
                while (getinput2 != 0)    
                {    
                    temp = getinput2;    
                    getinput2 = getinput1 % getinput2;    
                    getinput1 = temp;    
                }    
    
                hcf = getinput1;    
                lcm = (x * y) / hcf;    
    
    
                Console.WriteLine("Greatest Common Divisor of " + x + " & " + y + " is:" + hcf);    
                Console.WriteLine("Least Common Multiple of " + x + " & " + y + " is:" + lcm);    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Decimal to Binary")    
            {    
                Console.WriteLine("Enter one number:");    
                /* Method 1*/    
                int getinput1 = Convert.ToInt32(Console.ReadLine());    
    
                string binary = Convert.ToString(getinput1, 2);    
                Console.WriteLine("Decimal to Binary of " + getinput1 + "by method1 is:" + binary);    
                /* Method 1*/    
                /* Method 2*/    
                const int mask = 1;    
                var binaynum = string.Empty;    
                while (getinput1 > 0)    
                {    
                    // Logical AND the number and prepend it to the result string    
                    binaynum = (getinput1 & 1) + binaynum;    
                    getinput1 = getinput1 >> 1;    
                }    
                Console.WriteLine("Decimal to Binary of same by method2 is:" + binary);    
                /* Method 2*/    
    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Add Numbers")    
            {    
                Console.WriteLine("Enter two numbers:");    
    
                int getinput1 = Convert.ToInt32(Console.ReadLine());    
                int getinput2 = Convert.ToInt32(Console.ReadLine());    
                int sum = 0;    
                for (int i = getinput1; i <= getinput2; i++)    
                {    
                    sum = sum + i;    
                }    
                Console.WriteLine("Addition of n numbers is:" + sum);    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Swap")    
            {    
                Console.WriteLine("Enter two number:");    
    
                int getinput1 = Convert.ToInt32(Console.ReadLine());    
                int getinput2 = Convert.ToInt32(Console.ReadLine());    
    
                int x, y;    
                x = getinput1;    
                y = getinput2;    
    
                /*Method 1->  using third variable*/    
                int temp;    
    
                temp = getinput1;    
                getinput1 = getinput2;    
                getinput2 = temp;    
    
                Console.WriteLine("Numbers swapped as:" + getinput1 + "& " + getinput2);    
                Console.ReadLine();    
                /*Method 1->  using third variable*/    
    
                /*Method 2-> without using third variable*/    
                x = y - x;   // a=> b-a    
                y = x + y;   // b=> a+b    
                x = x + y;  // a=>b-a    
    
                Console.WriteLine("Numbers swapped as:" + getinput1 + "& " + getinput2);    
                Console.ReadLine();    
                goto Restart;    
    
            }    
            else if (read == "Reverse")    
            {    
    
                Console.WriteLine("Enter string:");    
                string input1 = Console.ReadLine();    
                /* Reverse String Method 1*/    
                string reversest = string.Empty;    
                Stack stack = new Stack();    
                foreach (char data in input1)    
                {    
                    stack.Push(data);    
                }    
    
                foreach (var data in stack)    
                {    
                    reversest += data;    
                }    
                Console.WriteLine("Reverse string of:" + input1 + "is " + reversest);    
                /* Reverse String Method 1*/    
    
                /* Reverse String Method 2*/    
                char[] chrarray = input1.ToCharArray();    
                string reversestring = string.Empty;    
                for (int i = chrarray.Length; i >= 1; i--)    
                {    
                    reversestring += chrarray[i - 1];    
                }    
                Console.WriteLine("Reverse string of:" + input1 + "is " + reversestring);    
    
                /* Reverse String Method 2*/    
    
                Console.ReadLine();    
                goto Restart;    
    
            }    
            else if (read == "Armstrong")    
            {    
                Console.WriteLine("Enter Number:");    
                string getinput = Console.ReadLine();    
                char[] input1 = getinput.ToCharArray();    
    
                int length = input1.Length;    
    
                double x = Convert.ToDouble(getinput);    
                double armstrong = 0;    
    
                for (int i = 0; i <= length - 1; i++)    
                {    
                    string num = input1[i].ToString();    
                    int number = Convert.ToInt32(num);    
    
                    armstrong = armstrong + Math.Pow(number, length);    
                }    
    
                if (armstrong == x)    
                {    
                    Console.WriteLine("Yes! it's an Armstrong number.");    
                }    
                else    
                {    
                    Console.WriteLine("No! it's not an Armstrong number.");    
                }    
    
                Console.ReadLine();    
                goto Restart;    
            }    
    
            else if (read == "Fibonacci")    
            {    
                Console.WriteLine("Enter two range as numbers:");    
    
                int getinput1 = Convert.ToInt32(Console.ReadLine());    
                int getinput2 = Convert.ToInt32(Console.ReadLine());    
    
                string fibonacci = string.Empty;    
                int temp = getinput1;    
                fibonacci = temp.ToString() + "," + getinput2.ToString();    
    
                for (int i = 0; i <= 10; i++)    
                {    
    
                    temp = getinput1 + getinput2;    
                    getinput1 = getinput2;    
                    getinput2 = temp;    
                    fibonacci += "," + getinput2;    
                }    
    
                Console.WriteLine("Fibonacci Series between two numbers is:" + fibonacci);    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Blank Space")    
            {    
                Console.WriteLine("Enter string:");    
    
                string getinput1 = Console.ReadLine();    
    
                /* Simple in c# due to function -> Replace*/    
                string newstring = getinput1.Replace(" ", "");    
                Console.WriteLine("String after removing blank space:" + newstring);    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Floyds Triangle")    
            {    
                for (int i = 0; i <= 20; i++)    
                {    
                    for (int j = 0; j <= i; j++)    
                    {    
    
                        Console.Write(j + " ");    
    
                    }    
                    Console.Write("\n");    
    
                }    
                Console.ReadLine();    
                goto Restart;    
            }    
            else if (read == "Pascal Triangle")    
            {    
                for (int i = 1; i <= 10; i++)    
                {    
                    for (int j = 1; j <= 10 - i; j++)    
                    {    
                        Console.Write(" ");    
    
                    }    
                    //for (int k = 20; k >= i; k--)    
                    //{    
                    //    Console.Write(" " + k);    
                    //}    
                    for (int k = 1; k <= i; k++)    
                    {    
                        Console.Write(" " + k);    
                    }    
    
                    Console.Write("\n");    
    
                }    
                Console.ReadLine();    
            }    
        }    
    
    }    
}    

Friday, 19 February 2016

How to scroll the HTML Page Title ?

There are various ways in html to scroll the title in which, one of the way is as shown:

Add the script to head tag in script blog:



        var scrl = " My ASP.NET Application ";
        function marqtitle() {
            scrl = scrl.substring(1, scrl.length) + scrl.substring(0, 1);
            document.title = scrl;
            setTimeout("marqtitle()", 500);
        }

    
And from body call onload="marqtitle()"

That's it done.

Thursday, 18 February 2016

How to get result from ASP.NET Web API in either JSON or XML Format?

Situation may exist some time as we want the data from API in JSON or either in XML format. So how to achieve that in ASP.NET Web API?

Here is the solution:

Add Namespace:

using System.Net.Http.Formatting;
using System.Net.Http.Headers;

Go to ->  Global.asax file and Add following script to Application_Start().

For JSON :
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
    new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));

For XML :
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
    new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));


Now use query string while calling the API.

as ::

             for xml : http://localhost:49533/api/?type=xml

             for json: http://localhost:49533/api/?type=json
             

That's it this is how we can get data as we want in XML or JSON.

Friday, 12 February 2016

How to use Web Grid in ASP.NET MVC?

Web Grid is easy to use for those who are having complexity in various operations on ASP.NET Table or List View like Sorting, Paging etc.

Let use see how web grid works.

 Here we're having a view model that contains following data. We'll get the data and bind to Web Grid.





































  Now in controller create a ActionResult method in which we'll even call repository method for getting the data from database.



Repository Class Method:
























   
Now once you get the data as shown in Controller store the data in ViewBag and return to View.

In View get that list and bind to grid as shown.



















WebGrid object=new WebGrid(list);
                            
       where: list is Generic List. 

Now it's really interesting and easy to do Sorting, Paging in grid.

  1. Sorting is by default available in grid headers. If you want to disable you can use as:
WebGrid grid=new WebGrid(list,canSort:false);

      2. For paging :


WebGrid grid=new WebGrid(list,rowsPerPage:5);

      3. Display Column you want:

 WebGrid grid = new WebGrid(list,  columnNames: new[] { "E_Name", "E_Contact" });

      4. Change Column Header Name as shown in GetHtml():

@grid.GetHtml(columns:grid.Columns(grid.Column("E_Name","Name"),grid.Column("E_Contact","Contact Number")));



One can also perform many other operations this were the basic of them.
Thanks.

Saturday, 6 February 2016

How to handle multiple button in single view and same form on submit in ASP.NET MVC?

There might be situation where you have to use multiple button in same form. Then what would you do ?

  If you don't want to mess up more in this, then let me scroll you to our main topic and show you how to do it.





































Now you can see in code above I have given  all 4 buttons same name as command.

 <button type="submit" class="btn btn-default" name="command" id="btnMonth" value="Month" style="margin-top:25px;">Submit</button>

<button type="submit" class="btn btn-default" name="command" id="btnDay" value="Day" style="margin-top:25px;">Submit</button>

And on form submit call action as shown:

@using (Html.BeginForm("MultipleCommand", "Default", FormMethod.Post, new { id = "Form1" }))
{
}

So on button click MultipleCommand of Default controller would be called and we can use it in the as shown below.



That's it Over.

Use if else condition as if command =="blabla" {// Your code to execute } and finally return as you want.

Thanks.

How To Pass An Array As A Parameter While Calling an ASP.NET Web API C#

Please visit my C# Corner Blog for this, where I have provided better efforts to make this concept more understandable - www.c-sharpcorner....