Thursday 27 September 2012

MVVM


Models are simple class objects that hold data. They should only contain properties and property validation. They are not responsible for getting data, saving data, click events, complex calculations, business rules, or any of that stuff.

Views are the UI used to display data. In most cases, they can be DataTemplates which is simply a template that tells the application how to display a class. It is OK to put code behind your view IF that code is related to the View only, such as setting focus or running animations.

ViewModels are where the magic happens. This is where the majority of your code-behind goes: data access, click events, complex calculations, business rules validation, etc. They are typically built to reflect a View. For example, if a View contains a ListBox of objects, a Selected object, and a Save button, the ViewModel will have an ObservableCollection ObectList, Model SelectedObject, and ICommand SaveCommand.

Thursday 20 September 2012

wpf



In .Net framework 3.0, Microsoft introduced foundation concepts WPF (Windows Presentation Foundation) is one of the foundation technology concept by Microsoft with .Net framework 3.0
  • WPF means Windows Presentation Foundation
  • WPF is one of the type of application using this WPF template we can develop a windows application
  • WPF application is an extension of traditional wing forms applications
  • Using WPF application user can develop a rich user interface
  • Microsoft mainly concentrated on user interface in WPF
  • WPF application is integrated with rich user interface and 2D or 3D graphics and multimedia features
  • Because of all these features, .Net programmer can develop a beautiful user interface by using WPF
  • WPF application is reducing the programmer task while design windows page
  • They have introduced some additional controls in WPF
  • To make programmer task easy they have changed some of the property names
  • To develop a WPF application we can use WPF application template within the Visual Studio
  • Base namespace for WPF application programming is System.Windows
  • WPF application will contain 2 code windows
Ø  .xmal
Ø  .xmal.cs
  • .XAML: .XMAL means Xtensible Application Markup Language.
  • XAML file contains design part code which was developing a markup language called XAML.
  • Using XAML. We can design the windows page. We can control appearance of window page.

            Code:
                                    <Window>
                                                <Grid>

                                                            ----
                                                </Grid>
                                    </Window>

XAML:
           
  • XAML is a markup language, is base on XML
  • XAML is introduced by Microsoft. It was invented for WPF to create and initialize WPF user interface

.XAML.cs:

  • This code window is representing business logic
  • Here we can use any .Net language to develop the application

WPF User Interface:

  • In traditional windows application user interface is called as Form
  • In WPF application user interface is called as Window.
  • Within that window we will have one more container called Grid
  • This Grid is used to design the WPF windows page with the controls.


Example for Simple WPF application:

Step 1: Open a WPF application using Visual Studio
           
            Name the window as employeeinformation


Step 3: Implement the below code on btndisplay_click event

                        Private void btndisplay_click(- -)
                        {
                                    textName.Text = “Shankar”;
                                    textDest.Text = “Software Engineer”;
                        }


Example: To retrieve the data from the database to display within the Grid view control of WPF window.

                        First create a database table with name emp

           
Empno
Ename
Sal
111
Shankar
7000
222
Pramod
8000
333
Laxman
9000

Step1: Goto SQL Server database and create a table

-         Create table emp(Empno int primary key, Ename varchar(15), Sal int)
-         Press F5 for execute
-         Insert into emp values(111, ‘Shankar’, 7000)
-         Insert into emp values(122, ‘Pramod’, 8000)
-         Insert into emp values(333, ‘Laxman’, 9000)

Step 2: Opening a WPF application using Visual Studio

            Note: Grid view control is not available in WPF application
           
-         Whenever we want to use Gridview control in WPF application we can create the Gridview control within the Listview control
-          
-         Drag and drop the listview control
-         S
-         Drag and drop the button control
-         S
-         Write the below code to create gridview control in .xaml file

Nt: Within the listview control we have to write the code

.xaml file:

            <Listview Margin=”48,60,0,102”, Name=”Listview1” HorizontalHeight =”Left”
                                                            Width=”203” itemsource=”{Binding path=Table}”>
            <Listview.view>
                        <Gridview>
                                    <Gridview column Header = “Empno”
                                                DisplayMemberBinding=”{Bindingpath=Empno}”>
                                    </Gridview column>

                                    <Gridview column Header = “EmpName”
                                                DisplayMemberBinding=”{Bindingpath=Ename}”>
                                    </Gridview column>


                                    <Gridview column Header = “Salary”
                                                DisplayMemberBinding=”{Bindingpath=Sal}”>
                                    </Gridview column>
                        </Gridview>
            </Listview.view>
</Listview>

 Code for button:

                        <Button Height = “23” Margin=”90,0,113,50”; Name=”Button”
VerticalAlignment= “Bottom” click=”button1_click”>Display
                        </Button>
                        </Grid>
                        </Window>
           
Write code in .xaml.cs file in btndisplay_click:

            Add namespaces:
                 
                  Using System.Data;
                  Using System.Data.Sqlclient;

                  public void button1_click(--)
                  {
                              string cs = “server=SHANKAR-PC; database=ltc_gamble; uid=sa; pwd=456321aA”;

                              sqlconnection conn = new sqlconnection(cs);

                              sqlcommand cmd = new sqlcommand(“Select * from emp”, conn);

                              sqlDataAdapter da = new sqlAdapter(cmd);

                              Dataset ds = new Dataset( );

                              da.Fill(ds, “empnew”);
     
                              listview1.Datacontrol = ds.Tables[0].Default.view;
                  }

Example for Inserting the data into a table within the SQL server using WPF application:

Step1: Open a WPF application using Visual Studio

      Drag and drop the 3 labels, 3 textboxes, and 1 button :

Step 2: Write below code within the btnInsert_click of .xaml.cs file

                  Using System.Data;
                  Using System.Data.Sqlclient;

                  Private void btninsert_click(--)
                  {
                              int empno = Covert.Toint32(texteno.Text);
                              string ename=textename.Text;
                              int sal = Convert.Toint32(textsal.Text);

                              string cs = “server=SHANKAR-PC; database=ltc_gamble; uid=sa; pwd=456321aA”;

                              sqlconnection conn = new sqlconnection(cs);

                              conn.open( );

sqlcommand cmd = new sqlcommand(“Insert into emp values(@empno, @ename, @sal)”, conn);

                              cmd.Parameters.Addwithvalue(“@empno”, empno);
                              cmd.Parameters.Addwithvalue(“@ename”, ename);
                              cmd.Parameters.Addwithvalue(“@sal”, sal);

                              cmd.ExecuteNonQuery( );

                              texteno.clear( );
                              textename.clear( );
                              textsal.clear( );

                              label.content = “Registration successfully”;

                  }
                              

Wednesday 19 September 2012

How to create wcf service in asp.net



 
In .Net framework 3.0, Microsoft introduced foundation concepts WCF (Windows Communication Foundation) is one of the foundation technology concept by Microsoft with .Net framework 3.0
  • WCF is an extension of XML Web Services
  • Using WCF application, we can develop a Web services, that means WCF application is a web application which is provides services to other applications over the Internet. So, finally WCF concept is providing a facility to a .Net programmer to develop a advanced technology web service.
  • Microsoft is providing a separate template to develop a WCF application from .Net framework 3.0 onwards.
  • WCF service will provide more security than traditional web services in .Net
  • WCF service application can be consumed by all the type of applications and all the technology applications
  • In .Net WCF application implementation will be divided into 2 phases:

1)      Creating a WCF service
2)      Creating a client application and consuming WCF service

System.ServiceModel is the base namespace for WCF application

Using WCF service application template we can develop a WCF service

By default WCF service application will come with 2 code windows

1)      IService1.cs
2)      Service1.svc

  • Extension of WCF application is .svc
  • WCF service application will provide one interface called Iservice1, which will contain some default members and this interface is representing with name called IService1.cs

                        class service1: Iservice1
{         
---
}

Interface Iservice1
{
            //default members
}



Ø  This service1 class is representing in a file called Service1.svc
Ø  Iservice1 interface and service1 class will come with some default members. If we required we can also use that members in our development
Ø  When we are developing WCF service application the development process can be divided into  2 steps
o   According to our requirement we have to declare the required member functions and properties within the Iservice1 interface
o   All the declared members we have to define(implement) within the service1 class, then build the application

Contracts:

Ø  WCF services are exposing with contracts
Ø  Contracts will define what services a client can consume
Ø  Contracts are nothing but attributes
Ø  Contracts are:
1)      [Service Contract]: It will be exposing with interface level

2)      [Operation Contract]: It will be exposing with method level

3)      [Data Contract]: It will be exposing with custom define data types

4)      [Data Member]: It will be exposing with fields, properties and events

Note: When we are declaring a member within the Iservice1.cs file, we have to expose with the concern contract.
Ø  If we didn’t mention the concern contract for a method or property, our WCF service will not include that member as a part of application

Step by Step process to create a WCF service application and Creating a client applicaton and consuming the WCF service:

Ø  Open a WCF service application using Visual studio
Ø  You can identify 2 code windows within the solution explorer
Ø  Open the Iservice1.cs file and declare the members according to our requirement
                        // Todo: Add your service operations here

[Operation contract]

int Add(int a, int b)
           
            [Operation contract]

                        string Hello(string name);

int Multiply(int a, int b)   - Here not adding contract
Step 2: Goto solution explorer and open the service1.svc file. In that file, we will have a class called service1.

            - Implement my members

                        public int Add(int a, int b)
                        {
                                    return a+b;
                        }
                       
                        public string  Hello(string name)
                        {
                                    return “welcome” + name;
                        }

                        public int Multiply(int a, int b)
                        {
                                    return a*b;
                        }
- Now build the application


Step3: Creating client application

            - Opening a windows application using Visual Studio
            - Name it as wcfclient
            - Design the windows form as for your requarement.

here i have taken text boxes for calculating the numbers.


Step 4: Adding service references

Goto Solution Explorer à Select References à Right click à Add Service References

                                                It will open Add service reference window

Go back to WCF service application and run it à Ok

Browser window will display the

            Copy the URL of the WCF service

Comeback to client application and paste that URL within the address textbox of Add Service Reference Window

            Click the Go button

            Click Ok

            Your Solution explorer will display the WCF service reference file

Step 5: Creating object for WCF proxy class

            (Write after Initialize component( ) )

                        Servicereference1.serviceclient wcfobj = new
                                                                           wcfclient.servicereference1.serviceclient( )

Step 6: Write the below code within the btnAdd_click event procedure

                        private void btnAdd_click(--)
                        {
                                    textResult.Text=wcfobj.Add(Covert.Toint32(textNum1.Text),
                                                                                    Convert.Toint32(textNum2.Text)).ToString( );
                        }

            Write the below code within the btnSubmit_click

                        private void btnsubmit_click(--)
                        {
                                    textGreetings.Text=wcfobj.Hello(textName.Text);
                        }

            Build the application
 Note: if you have any doubts in this article please drop a comment,i will try to resolve .

How to create web services in asp.net


Hi,today i am going to explain about creating simple web services in asp.net. i hope it will help for beginner's of asp.net.i am going to explain step-by-step manner ,just fallow the lines what i have written below. 

  • Web services are web applications only, they are providing services to some other applications over the Internet.
Eg: All Search Engines, Currency Converters
  • Web service is one of the type of application, it is depending on Internet connectivity to function that’s why we are calling it as a web application
  • It is an application which is providing services through web, that’s why we are calling it as web services

  • In general web services implementation will be divided into 2 phases:
Phase 1:  Creating a web service according to our requirement
Phase 2:  Creating client application to consume the web service.

                        Here web service is a service provider; client application is a service receiver.
beginer
Purpose of Web Services:

1)      Reusing the Component: Whenever we are using the same component in multiple applications instead of creating the same component in all the applications, we will create a web service for that component and we will consume that web service from the ‘n’ no. of applications.

Eg: Consuming the currency converter from multiple website

2)      Exchanging the data between applications: Whenever we want to exchange the data between two applications we will use the web services.

A web service can consumed by any type of application like Console, Windows, Web application,..etc.,

In .Net if we want to develop the web services we have a template called “ASP .NET Web Service Application”

By default ASP.NET Web Service application is coming with a one class called Service1

Web service is a pre-defined class which is base class for the user defined service classess

                        Class service1 : web service
                        {
                                    ---
                        }

Web service class is a part of System.Web.Services name space

                        namespace services
                        {
                                    class webservices
                                    {
                                                ---
                                    }
                        }

In .Net web service file will be represented with .asmx

Web service will representing with platform independent component because it can provide services to all the type of applications over the network with respect to of operating systems.

This component is a collection of web methods

Web Method: The method which is providing services to other applications is called as Web method.

            A Web service class is a collection of web methods.


Working with Web Services:

                        It is divided into 2 phases:
           
            Phase 1: Creating a web service in step by step process
           
            Phase 2: Creating a client application and consuming the web service

Step by step for creating a Web Service:

Step 1: Open a Web Service application using ASP.NET Web application template

            Create Project à Visual Studio C# à ASP.NET Web application

-         Removing the existing “Hello World” method

Step 2: Implementing web methods according to our requirement [In this we use 2 methods Add( ) and Sub( )]

                        [ web method]

                        Public int Add(int a, int b)
{
            returen a+b;
}

[web method]

Public int sub(int a, int b)
{
            return a-b;
}

-         Build the application

Step 3:  Creating a client application
           
-         Open a windows application
o   Create project à Visual C# à Windows application à Ok

-         Design clientapps windows from.




Step 4: According the web service reference to client application

-         Goto Solution Explorer of the client application
o   Select References à Right Click à Add service reference à Ok
                       

-         Go back to Web services application window and run the web service application window
-         Here you may get a message box à Click Ok
-         Then it will open the web services page within the browser window
-         Goto the browser window address bar and Copy the url of the web service
-         Now go back to the client application window and Paste that url within the Add service reference window within the address bar.
-         Now click Ok

-         Now client application solution explorer window is representing a new file called Service references. It is representing the our web service.

Step 5: Consuming the web service

-         Gotot the client application code window, create object for web reference

Code within form_load( )

            Public partial class form1 : form
            {
                        Public form1( )
                        {
                                    Initialize component( );
                        }

Service reference1.Service1Soapclient serobj = new clientapp.servicereference1.service1Soapclient( );
Implement the btnAdd client event:

            Double click on Add button and write code as:

            Private void btnAdd_click( )
            {
textResult.Text =          serobj.Add(Convert.Toint32(textNum1.Text),
Conver.Toint32(textNum2.Text)).ToString( );\
                        }

            Implement the btnSub client event:

                        Double click on Sub button and write code as:

            Private void btnSub_click( )
            {
textResult.Text =          serobj.Sub(Convert.Toint32(textNum1.Text),
Conver.Toint32(textNum2.Text)).ToString( );\
                        }

-         Build the application and run the application

Ø  In .Net Web Services are integrated with XML, thatswhy we are calling it as XML web services
Ø  In Web services the data which we will use will be representing in the form of XML
Ø  The data will be coding and decoding from XML file
Ø  XML is a platform independent and language independent technology, automatically web services applications are also platform independent and language applications
Ø  In .Net web services are based on SOAP (Simple Object Access Protocol). Soap is communication protocol and it is called as open protocol. SOAP is based on XML. SOAP is acting as a mediator between .Net Web Service and Client application
Ø  That means the data transportation responsibility between Web Services and Client taking by SOAP