Tuesday, April 17, 2012

Updating any Object through Linq Query



public class TestClass
{
    public String RollNo { get; set; }
    public String Name { get; set; }
    public String ClassName { get; set; }
}
ObservableCollection<TestClass> aaa = new ObservableCollection<TestClass>();

aaa.Add(new TestClass() {
RollNo = "1602", Name = "Khalid Rafique", ClassName = "A" });

aaa.Add(new TestClass() {
RollNo = "1702", Name = "Jamal Ali Khan", ClassName = "A" });


Method1

var result = aaa.Where(c => c.RollNo == "1702");
foreach (TestClass tc in result)
{
tc.Name = textBox4.Text;
      tc.ClassName = textBox1.Text;
}

Method2

aaa
    .Where(c => c.RollNo == "1702")
    .Update(m =>
                {
                  m.Name = textBox4.Text;
                  m.ClassName = textBox1.Text;
                });


Following is the class having extensible Method of Linq update.

public static class LinqExtensableMethods
{
public static void Update(this IEnumerable source, params Action[] updates)
{
    if (source == null)
      throw new ArgumentNullException("source");

    if (updates == null)
      throw new ArgumentNullException("updates");

       foreach (T item in source)
       {
                foreach (Action update in updates)
                {
                    update(item);
                }
       }
   }
}

Method3

var result =  ocTextClass .Where(c => c.RollNo == "1702").ToList().ForEach(tc => tc.Name=TextBox.Text);

Monday, April 16, 2012

Creating Cut, Copy, Paste Context menu for all textboxs in silverlight using Style in App.xaml

This article will provide you the help for creating Cut, Copy, Paste Context menu for all textboxs in silverlight using Style in App.xaml. This style will work for every Textbox that you will add in any form of your application..

App.xaml


xmlns:my="clr-namespace:YourApplicationNameSpace"



<Application.Resources>
  <Style TargetType="TextBox">
     <Setter Property="my:ControlCustomBehaviors.SetMenu" Value="true" />
  Style>
Application.Resources>

ControlCustomBehaviors.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;


namespace YourApplicationNameSpace
{

public class ControlCustomBehaviors : Behavior<Control>
{

#region Set Context Menu
public static DependencyProperty SetMenuProperty =
          DependencyProperty.RegisterAttached("SetMenu",
                                              typeof(object),
                                              typeof(ControlCustomBehaviors),                                            new PropertyMetadata(null, SetMenuProp));
       
private static TextBox tBox;
private static void SetMenuProp(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
 tBox = d as TextBox;
 CreateMenu();


 tBox.MouseRightButtonDown += new    MouseButtonEventHandler(AssociatedObject_MouseRightButtonDown);

 tBox.MouseRightButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseRightButtonUp);

 tBox.SetValue(ContextMenuService.ContextMenuProperty, _contextMenu);

}

public static object GetSetMenu(UIElement obj)
{
    return (object)obj.GetValue(SetMenuProperty);
}
public static void SetSetMenu(DependencyObject obj, object txtbox)
{
     obj.SetValue(SetMenuProperty, txtbox);
}

private static ContextMenu _contextMenu;
private static MenuItem _copy;
private static MenuItem _cut;
private static MenuItem _paste;

private static void CreateMenu()
{
  _contextMenu = new ContextMenu();
  _cut = new MenuItem();
  _cut.Header = "Cut";
  _cut.Click += new RoutedEventHandler(cut_Click);
  _contextMenu.Items.Add(_cut);

  _copy = new MenuItem();
  _copy.Header = "Copy";
  _copy.Click += new RoutedEventHandler(copy_Click);
  _contextMenu.Items.Add(_copy);

   _paste = new MenuItem();
   _paste.Header = "Paste";
   _paste.Click += new RoutedEventHandler(paste_Click);
   _contextMenu.Items.Add(_paste);
  }
  static void paste_Click(object sen, RoutedEventArgs e)
  {
    tBox.SelectedText = Clipboard.GetText();
    _contextMenu.IsOpen = false;
     tBox.Focus();
  }
  static void cut_Click(object sen, RoutedEventArgs e)
  {
     Clipboard.SetText(tBox.SelectedText);
     tBox.SelectedText = string.Empty;
     _contextMenu.IsOpen = false;
     tBox.Focus();
  }
  static void copy_Click(object sen, RoutedEventArgs e)
  {
      Clipboard.SetText(tBox.SelectedText);
      _contextMenu.IsOpen = false;
      tBox.Focus();
  }

  static void AssociatedObject_MouseRightButtonUp(object Sender,  MouseButtonEventArgs e)
        {
  if (Clipboard.ContainsText() && tBox.IsReadOnly == false && tBox.IsEnabled == true)
  {
     _paste.IsEnabled = true;
  }
  else
  {
   _paste.IsEnabled = false;
  }
  if (string.IsNullOrEmpty(tBox.SelectedText))
  {
    _cut.IsEnabled = false;
    _copy.IsEnabled = false;
  }
  else
  {
    _cut.IsEnabled = true;
    _copy.IsEnabled = true;
  }
  _contextMenu.IsOpen = true;
   tBox.Focus();

  }
  static void AssociatedObject_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
 {
    tBox = sender as TextBox;
    e.Handled = true;
 }
 #endregion

}
}

Friday, April 13, 2012

How to find the nth Highest Salary from Employee Table

You can find out the nth Highest Salary from the Employee Table by using following systax

select top 1 * from testEmp
where Emp_Id not in
(select top (n-1) Emp_Id from testEmp order by salary desc)
order by salary desc

Let Suppose following is you table

Emp_Id
Employee_Name
Salary
1602
Khalid Rafique
40000
1650
Jamal Ali Khan
45000
1530
Farhan Khan
29000
1533
Abid sultan
33000
1535
Atiq Khan
38000
1730
Rashid Ali
21000

if you want to find 2nd highest salary, you need to have to write the following query

select top 1 * from testEmp 
where Emp_Id not in
(select top (2-1) Emp_Id from testEmp order by salary desc)
order by salary desc

Result:
Emp_Id
Employee_Name
Salary
1602
Khalid Rafique
40000


if you want to find 3rd highest salary, you need to have to write the following query
select top 1 * from testEmp 
where Emp_Id not in
(select top (3-1) Emp_Id from testEmp order by salary desc)
order by salary desc


Result:
Emp_Id
Employee_Name
Salary
1535
Atiq Khan
38000

if you want to find 4rd highest salary, you need to have to write the following query

select top 1 * from testEmp 
where Emp_Id not in
(select top (4-1) Emp_Id from testEmp order by salary desc)
order by salary desc


Result:
Emp_Id
Employee_Name
Salary
1533
Abid sultan
33000

How to find the dupliate record in sql w.r.t columns

You can find the duplicate record using the folloing query with respect to columns sal and age
if you have Users table
LoginId Password UserName Sal Age
admin 1153291460 Khalid Rafique 1100.00000000 25
asdf tyro1 aa 1200.00000000 28
tyro tyro1 Test Account 1200.00000000 28
tyro23 tyro1 Test Account 1200.00000000 28

One Way
select u1.* from Users u1 
inner join
  (select Sal, Age,COUNT(*)as [Count] from Users
   group by Sal,Age
   having COUNT(*)>1) 
as t1
on u1.Sal = t1.Sal and u1.Age =t1.Age


2nd Way
select * from Users
where CAST(Sal as varchar)+'-'+CAST(Age as varchar) in
(select CAST(Sal as varchar)+'-'+CAST(Age as varchar) from Users
 group by Sal,Age
 having COUNT(*)>1)


following will be the result of Query:

LoginId
Password
UserName
Sal
Age
asdf
tyro1
aa
1200.00000000
28
tyro
tyro1
Test Account
1200.00000000
28
tyro23
tyro1
Test Account
1200.00000000
28

Tuesday, April 3, 2012

How to create own Grid property binder class in silver light

    
public static class GridPropertyBinder
{
  #region Visibility
  public static readonly DependencyProperty VisibilityBindingProperty = 
            DependencyProperty.RegisterAttached(
                                                "VisibilityBinding",
                                                typeof(object),
                                                typeof(GridPropertyBinder),
                                                new PropertyMetadata(null,
GridPropertyBinder.VisibilityBinding_PropertyChanged));

  public static object GetVisibilityBinding(DependencyObject source)
  {
     return (object)source.GetValue(GridPropertyBinder.VisibilityBindingProperty);
  }
  public static void SetVisibilityBinding(DependencyObject target, 
                                          object value)
  {
     target.SetValue(GridPropertyBinder.VisibilityBindingProperty, value);
  }
  private static void VisibilityBinding_PropertyChanged(DependencyObject d, 
                                                        DependencyPropertyChangedEventArgs e)
  {
     DataGridColumn column = d as DataGridColumn;
     if (column == null) { return; }
     if ((bool)e.NewValue)
     {
       column.Visibility = Visibility.Visible;
     }
     else
     {
        column.Visibility = Visibility.Collapsed;
     }
  }
  #endregion
}