Wednesday, February 22, 2012

WPF - Two Way Textbox Binding in WPF

In WPF, one of the important features provided is to bind one XAML element to another element, without using any code. This reduces code-behind requirements for the XAML file.In this article i will explained Two-Way binding between Textboxes in a WPF application, using the UpdateSourceTrigger property of the Binding class.


Step 1
Create a Wpf Application and give the solution name as SolTwoWayBindingWPF.


Step 2
Open MainWindow.Xaml and add two textboxes. Since we need to bind these textboxes with two-way DataBinding,it is look like this
<Grid>
        
        <TextBox x:Name="txtbox1"  Height="23" HorizontalAlignment="Left" 
         Margin="76,115,0,0"  VerticalAlignment="Top" Width="358" />
        
        <TextBox x:Name="txtbox2" Text="{Binding ElementName=txtbox1,Path=Text,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
         Height="23" HorizontalAlignment="Left" Margin="76,173,0,0"  
         VerticalAlignment="Top" Width="358" />
        
    </Grid>


we need to set the following properties of the Binding class:
  • Mode: Used to define the Strategy for the data binding with following values:
    • OneWay
    • TwoWay
    • OneWayToSource etc.
  • ElementName: The source WPF element name
  • Path: The Dependency Property of the Source WPF Element
  • UpdataSourceTrigger: The Event to be raised on the source element dependency property.

Step 3
Run the application, type some text in the first textbox and you will observe that the same text automatically replicates in the second textbox. Similarly, type some text in second textbox and the same text gets added in the first textbox.


Output
Click on image for better view

Download

Tuesday, February 21, 2012

MS-SQL - Create Job in Sql Server 2008

In this article i will show you how to create a Job in Sql Server 2008.


Step 1
Download Northwind database from the following link.
http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034&displaylang=en


Step 2
Attach a Northwind database into MS-SQL server.


Step 3
Expand the SQL Server Agent. You will see a Jobs folder over there. Right click on jobs and choose Add New.,it is look like this






Click on Image for better view


Step 4
Create a New Sql Server Agent Job,it is look like this




Click on Image for better view



New Job creation window shown above. Fill in the Name field with a unique name for your job. Specfy the account that you wish to be the owner of the job in the Owner text box. The job will run with the permissions of this account and may only be modified by the owner or sysadmin role members. 


Once you've specified a name and owner, choose one of the predefined job categories from the drop-down list. For example, you might choose the "Database Maintenance" category for routine maintenance jobs but for this example select only Uncategorized [local] only.


Finally, ensure that the Enabled box is checked.


Step 5
Enter the SQL Server Agent Job Steps Screen,it is look like this




Click on Image for better view


Next click on the ‘Steps’ pane on the left and you’ll be presented with this screen.  It’s blank because you haven’t created any steps yet.  So go ahead and click on the ‘New’ button at the bottom.


Step 6
Add SQL Server Agent Job Steps,it is look like this




Click on Image for better view



After Clicking  the New button to create a new job step and you will see the New Job Step window shown above.


Use the Step Name textbox to provide a descriptive name for the Step. 


Use the Database drop-down box to select the database that the job will act upon. 


Finally, use the Command textbox to provide the Transact-SQL syntax corresponding to the desired action for this job step. Once you have completed entering the command, click the Parse button to verify the syntax and then click on OK button.


In this job i am taking backup Northwind database to the disk.
BACKUP DATABASE Northwind
TO DISK = 'd:\NorthwindBackUp.bak'


Step 7
Create Schedule the SQL Server Agent Job,it is look like this




Click on Image for better view


To schedule your job, just click on ‘Schedules’ on the left and then choose the schedule that’s right for you then click on new button.

Step 8
Add Schedule the SQL Server Agent Job,it is look like this


Click on Image for better view

After Clicking  the New button to create a new job schedule and you will see the New Job Schedule window shown above.

Provide a name for the schedule in the Name text box and choose a schedule type (One-time, Recurring, Start when SQL Server Agent Starts or Start When CPUs Become Idle) from the drop-down box. Then use the frequency and duration sections of the window to specify the job's parameters. When you are finished click OK to close the Schedule window and OK to create the job.

Your job is ready now.

Step 9
For the testing purpose execute job first time,it is look like this


Click on Image for better view

Right Click on the Job and click on Start job at Step and you will see the Start Jobs window will open,it is look like this


Monday, February 20, 2012

C#.net - Lambda Expression in C#.net

What is Lambda Expression?
Lambda Expressions, using the Lambda Operator => were introduced in C# 3.0.A lambda expression is an anonymous function or simply a method that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read “x goes to x times x.”


We can use lambdas in method based LINQ query as argument to standard query. We cannot use lambda operators on the left side of is or as operator.


Let see following example of Anonymous Methods and Lambda Expression.

Step 1
Create a Console Application and give the solution name as ConLambdaExpression.

Step 2
Create a Developer Class in the solution,it's look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConLambdaExpression
{
    public class Developer
    {
        #region Property

        public String DeveloperName
        {
            get;
            set;
        }

        public String Speciality
        {
            get;
            set;
        }

        public int Experience
        {
            get;
            set;
        }

        #endregion
    }  
}


Step 3
Now add developer data in to list object in Main function,it is look like this

List<Developer> DeveloperObj = new List<Developer>();

                // Add Developer Data to the list object  
                DeveloperObj.Add(new Developer()
                {
                    DeveloperName = "Bhushan Pawar",
                    Speciality = "ASP.net",
                    Experience = 5
                });

                DeveloperObj.Add(new Developer()
                {
                    DeveloperName = "Ramdas Bhosale",
                    Speciality = "ASP.net",
                    Experience = 4
                });

                DeveloperObj.Add(new Developer()
                {
                    DeveloperName = "Koste Budinoski",
                    Speciality = "ASP.net MVC",
                    Experience = 7
                });

                DeveloperObj.Add(new Developer()
                {
                    DeveloperName = "Kishor Naik",
                    Speciality = "WPF & Silverlight",
                    Experience = 4
                });

                DeveloperObj.Add(new Developer()
                    {
                        DeveloperName="Vikas Bharti",
                        Speciality="SQL Server",
                        Experience=7
                    });

                DeveloperObj.Add(new Developer()
                    {
                        DeveloperName="Shashikant Patil",
                        Speciality="ASP.net",
                        Experience=3
                    }
                    );

                DeveloperObj.Add(new Developer() 
                {
                    DeveloperName = "Shilpa Gawde",
                    Speciality="WPF",
                    Experience=3
                }
                    );


Step 4
Anonymous Methods
C# 2.0 provides a new feature called Anonymous Methods, which allow you to create inline un-named ( i.e. anonymous ) methods in your code, which can help increase the readability and maintainability of your applications by keeping the caller of the method and the method itself as close to one another as possible.


To find a data from list object using Anonymous Methods,it's look like this 
// Search Data using Anonymous Method

                var AnonymousQuery = DeveloperObj.Where(
                     delegate(Developer D)
                     {
                         return D.DeveloperName.ToLower().Contains("vikas");
                     }
                    );

                System.Console.WriteLine("Anonymous Method");
                foreach (Developer DObj in AnonymousQuery)
                {
                    System.Console.WriteLine("Developer Name\t:\t" + DObj.DeveloperName);
                    System.Console.WriteLine("Speciality\t:\t" + DObj.Speciality);
                    System.Console.WriteLine("Experience\t:\t" + DObj.Experience);
                }

                System.Console.WriteLine();


Step 5
To find data from list object using Lambda Expression,it's look like this
//Search Data Using Lamda Expression

                var LambdaQuery = DeveloperObj.Where(LE => LE.DeveloperName.ToLower().Contains("ramdas"));

                System.Console.WriteLine("Lambda Expression");
                foreach (Developer DObj in LambdaQuery)
                {
                    System.Console.WriteLine("Developer Name\t:\t" + DObj.DeveloperName);
                    System.Console.WriteLine("Speciality\t:\t"+DObj.Speciality);
                    System.Console.WriteLine("Experience\t:\t"+DObj.Experience);
                }

Run the Project.


Output


Click on Image for better View


Full Main Function Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConLambdaExpression
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                List<Developer> DeveloperObj = new List<Developer>();

                // Add Developer Data to the list object  
                DeveloperObj.Add(new Developer()
                {
                    DeveloperName = "Bhushan Pawar",
                    Speciality = "ASP.net",
                    Experience = 5
                });

                DeveloperObj.Add(new Developer()
                {
                    DeveloperName = "Ramdas Bhosale",
                    Speciality = "ASP.net",
                    Experience = 4
                });

                DeveloperObj.Add(new Developer()
                {
                    DeveloperName = "Koste Budinoski",
                    Speciality = "ASP.net MVC",
                    Experience = 7
                });

                DeveloperObj.Add(new Developer()
                {
                    DeveloperName = "Kishor Naik",
                    Speciality = "WPF & Silverlight",
                    Experience = 4
                });

                DeveloperObj.Add(new Developer()
                    {
                        DeveloperName="Vikas Bharti",
                        Speciality="SQL Server",
                        Experience=7
                    });

                DeveloperObj.Add(new Developer()
                    {
                        DeveloperName="Shashikant Patil",
                        Speciality="ASP.net",
                        Experience=3
                    }
                    );

                DeveloperObj.Add(new Developer() 
                {
                    DeveloperName = "Shilpa Gawde",
                    Speciality="WPF",
                    Experience=3
                }
                    );


                // Search Data using Anonymous Method

                var AnonymousQuery = DeveloperObj.Where(
                     delegate(Developer D)
                     {
                         return D.DeveloperName.ToLower().Contains("vikas");
                     }
                    );

                System.Console.WriteLine("Anonymous Method");
                foreach (Developer DObj in AnonymousQuery)
                {
                    System.Console.WriteLine("Developer Name\t:\t" + DObj.DeveloperName);
                    System.Console.WriteLine("Speciality\t:\t" + DObj.Speciality);
                    System.Console.WriteLine("Experience\t:\t" + DObj.Experience);
                }

                System.Console.WriteLine();
                //Search Data Using Lamda Expression

                var LambdaQuery = DeveloperObj.Where(LE => LE.DeveloperName.ToLower().Contains("ramdas"));

                System.Console.WriteLine("Lambda Expression");
                foreach (Developer DObj in LambdaQuery)
                {
                    System.Console.WriteLine("Developer Name\t:\t" + DObj.DeveloperName);
                    System.Console.WriteLine("Speciality\t:\t"+DObj.Speciality);
                    System.Console.WriteLine("Experience\t:\t"+DObj.Experience);
                }


            }
            catch (Exception ex)
            {
                System.Console.WriteLine();
            }
        }
    }

   
}

Download
Download Source Code