Sunday, 29 November 2015

Creating and consuming web service in ASP.NET MVC


  • First of all we'll create a Web Service and then we'll consume the web service using ASP.NET MVC application.
  • This project is created on Visual Studio 2013.
  • Open a VS and select ASP.NET Empty web application as shown.



























  • Then add new item and select Web Service (.asmx) extension would be there, rename if you want. Finally it would be some thing like this.






















  • Above you can see a [web method] , actually this allows a  methods to call remotely and access the data.
  • When you'll run the program you'll see HelloWorld method which is created by default. Now there after you'll invoke the HelloWorld which will return output as "Hello World".





























  • In a same way we'll create a Simple Web Method for addition of two numbers named Calc which will return a string. One can go through below code , we've created new method Calc which takes 3 inputs:
      1. query:  For Add, Multiply, etc 
      2. num1: number
      3. num2: number
  • Here it's only shown for Addition you can do as you want.





  • Now after creating the web method you'll see Calc method would be created. For testing your created service you can go to Calc and click. 









  • After click input Add and two numbers as shown and click on invoke. Be sure it should show result as in XML format and addition of two numbers.























  • Now we'll consume a Web Service using ASP.NET MVC application. So create an Empty ASP.NET MVC Application as shown below. 




























  • Before consuming Web Service above created project should be running or hosted on IIS to access.
  • Then add that url in this Address and Press Go. Your hosted or running Web Service would be seen in Service Panel as shown.








  • For accessing the method give a reference of Web Service in namespace as shown.









  •  Thereafter create an object as shown by SoapClient and then give input to Web Method by calling the service and you'll get the output.


























  • And that's it Over.  You'll get your data in result and can use in View of MVC application.

Wednesday, 18 November 2015

Bubble Sorting Algorithm in C#


  • This article will teach you how use the bubble sort algorithm in c#. 
  • Yes, this may sometimes be asked in interview also.
  • First of all we'll see what is bubble sort algorithm.
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.[1] It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position.


ClassSorting algorithm
Data structureArray
Worst case performanceO(n^2)
Best case performanceO(n)
Average case performanceO(n^2)
Worst case space complexityO(1)auxiliary

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.
First Pass
5 1 4 2 8 ) \to ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) \to ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) \to ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) \to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass
1 4 2 5 8 ) \to ( 1 4 2 5 8 )
( 1 4 2 5 8 ) \to ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass
1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )

Now we'll try this kind of example in C#.  For implementing in bubble sort in C# here is the code:


Sunday, 15 November 2015

Create your own extension in Google Chrome Browser


  • Hi all, this article will teach you how to create an extension in Chrome browser. It's very easy you just have to do is follow my steps.
  • Now since Google Chrome works with JSON we'll create a .json extension file in one of the folder as shown.


















  • Now in that file enter following code and save it.

{
  "manifest_version": 2,

  "name": "Youtube Watcher",
  "description": "Watch Videos of youtube",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
  
}
  • Now create one html file named popup.html in same folder in which we'll enter following code:
<iframe width="560" height="315" src="https://www.youtube.com/watch?v=UcBn995_tZo" frameborder="0" allowfullscreen></iframe>
  • Thereafter we'll create one icon for youtube extension  named icon.png where you can save the youtube icon in this image file.
Note : All three files should be in Same folder as shown.




















  • Now open your Chrome browser and goto->More Tools->Extensions->Check the Developer Mode.
  • Now click Load unpacked extensions and select the folder where you have kept all this 3 files.
  • It will automatically load your youtube extension.
  • You can change anything you want if you are a json expert.











  • Thanks, I'll like share you this kind of stuff again.

Saturday, 14 November 2015

CRUD operations in ASP.NET MVC 5 using C#


  • This article is for beginners and it's for performing the CRUD operation in ASP.NET MVC 5 using C#. And in next article we'll see Searching, Paging and Sorting a table
  • Note :Download the Visual Studio 2013 or higher so you can grab all the latest features.
  • First create a new project in Visual Studio( * in this article 2013 is used) and give appropriate name to it.

  • Now after clicking OK one new window will prompt, where it will ask to choose the template. Simply select the empty template and check the MVC as shown in below picture.
  • Now after clicking OK, blank ASP.NET MVC solution will be created.

  • Before moving forward you need to create a database in Sql Server. I've created a database named TestDB and a table EmpDetails that contains EmpId, EmpName, EmpAdd, EmpNumber field.


  •  Now for connection of MVC to database we'll use Entity Framework . So we'll goto Tools-> Nuget Packet manager and install the Entity Framework package in our solution. After installation you will see this kind of view.

  • Now we'll add the Model(i.e. Class.cs) in Model Folder. Let us name it as EmpDetails.cs. And add namespace :
    1. using System.Data.Entity;
    2. using System.ComponentModel.DataAnnotations;
    3. using System.ComponentModel.DataAnnotations.Schema;
        • Now mention the table name and key as shown in Class file as per your need.



        • Now add a connection string to the web.config file as shown and make sure that the connection string name is same as of DBContext Entity.... here in my program it is EmpDBContext.




        • Now model has been created and we'll create a controller for action.But before creating the controller you've build your application once to effect the changes. So, here's a MVC 5 scaffholding part where you didn't have to do any code scaffholding will do by itself. You just have to do is select the controller by going to Controller Folder-> Add -> Controller-> Select MVC 5 Controller with views, using EntityFramework.
        • After selecting , window would be prompted which would ask for Model Class select your Model Class, then select the data context. And controller name would be taken automatically. Click Add. The scaffholding process would be going on which will create one controller named EmpDetailsController.cs in Controller Folder and simultaneously it will also create a View for EmpDetails controller where you would see the Create, Delete, Details, Edit and Index.cshtml files in Views-> EmpDetails folder as shown


        • Now just goto->AppStart->RouteConfig.cs and change the controller from Home to EmpDetails and run the applications.
        • You will get the output where there would be one table and you can Create , view delete the data of the table.
        This is how an easy to develop  Application in MVC 5 is done and is quite simpler. For understanding the details code you can go through class file generated in Controller. In next article we're going through that. Have a nice development.





        Tuesday, 3 November 2015

        Generate random numbers from List in c# using Linq


        • This article is to demonstrate how to generate the random number from the generic List<T> using Linq.
        • First of all we'll create the list of results which is in ascending order.
        • Then we'll select each record from the results, ordered by Guid each time.This gives randomness.
        • Thereafter we'll return the list.



        • Finally access the list however you want.

        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....