Saturday, 8 May 2021

Select and delete in single statement in SQL

Below SQL Query , Is to get the data after deleted from the table.

It can be used in the store procedure , in scenario of getting the data and deleting the data for same. So that when procedure run the thread then it will not lead to error due to no data present in the table .

Same thing can be achieved in two separate query with example below.

Begin tran t1

Select * from contacts where Id=2

Delete from contacts where Id = 2

Commit tran t1

Below is the operation ,  done in single query instead of two query

Begin tran t1

Delete from 

Contacts

output

Deleted.id,

Deleted.Name,

Deleted.Address,

Deleted.Phone

where id =2

Commit tran t1




Saturday, 27 March 2021

Angular Hook Life Cycle

Angular Hook Life Cycle: 

1.ngOnChanges

2.ngOnInit

3.ngDoCheck

4.ngAfterContentInit

5.ngAfterContentChecked

6.ngAfterViewInit

7.ngAfterViewChecked

8.ngOnDestroy

ngOnChanges

ngOnChanges triggers following the modification of @Input bound class members. Data bound by the @Input() decorator come from an external source. When the external source alters that data in a detectable manner, it passes through the @Input property again.

With this update, ngOnChanges immediately fires. It also fires upon initialization of input data. The hook receives one optional parameter of type SimpleChanges. This value contains information on the changed input-bound properties.

Example

import { Component, Input, OnChanges } from '@angular/core';

@Component({

  selector: 'app-child',

  template: `

  <h3>Child Component</h3>

  <p>TICKS: {{ lifecycleTicks }}</p>

  <p>DATA: {{ data }}</p>

  `

})

export class ChildComponent implements OnChanges {

  @Input() data: string;

  lifecycleTicks: number = 0;


  ngOnChanges() {

    this.lifecycleTicks++;

  }

}


@Component({

  selector: 'app-parent',

  template: `

  <h1>ngOnChanges Example</h1>

  <app-child [data]="arbitraryData"></app-child>

  `

})

export class ParentComponent {

  arbitraryData: string = 'initial';


  constructor() {

    setTimeout(() => {

      this.arbitraryData = 'final';

    }, 5000);

  }

}

ngOnInit

ngOnInit fires once upon initialization of a component’s input-bound (@Input) properties. The next example will look similar to the last one. The hook does not fire as ChildComponent receives the input data. Rather, it fires right after the data renders to the ChildComponent template.

Example:

import { Component, Input, OnInit } from '@angular/core';


@Component({

  selector: 'app-child',

  template: `

  <h3>Child Component</h3>

  <p>TICKS: {{ lifecycleTicks }}</p>

  <p>DATA: {{ data }}</p>

  `

})

export class ChildComponent implements OnInit {

  @Input() data: string;

  lifecycleTicks: number = 0;


  ngOnInit() {

    this.lifecycleTicks++;

  }

}


@Component({

  selector: 'app-parent',

  template: `

  <h1>ngOnInit Example</h1>

  <app-child [data]="arbitraryData"></app-child>

  `

})

export class ParentComponent {

  arbitraryData: string = 'initial';


  constructor() {

    setTimeout(() => {

      this.arbitraryData = 'final';

    }, 5000);

  }

}

ngDoCheck

ngDoCheck fires with every change detection cycle. Angular runs change detection frequently. Performing any action will cause it to cycle. ngDoCheck fires with these cycles. Use it with caution. It can create performance issues when implemented incorrectly.

ngDoCheck lets developers check their data manually. They can trigger a new application date conditionally. In conjunction with ChangeDetectorRef, developers can create their own checks for change detection.

Example:

import { Component, DoCheck, ChangeDetectorRef } from '@angular/core';


@Component({

  selector: 'app-example',

  template: `

  <h1>ngDoCheck Example</h1>

  <p>DATA: {{ data[data.length - 1] }}</p>

  `

})

export class ExampleComponent implements DoCheck {

  lifecycleTicks: number = 0;

  oldTheData: string;

  data: string[] = ['initial'];


  constructor(private changeDetector: ChangeDetectorRef) {

    this.changeDetector.detach(); // lets the class perform its own change detection


    setTimeout(() => {

      this.oldTheData = 'final'; // intentional error

      this.data.push('intermediate');

    }, 3000);


    setTimeout(() => {

      this.data.push('final');

      this.changeDetector.markForCheck();

    }, 6000);

  }


  ngDoCheck() {

    console.log(++this.lifecycleTicks);


    if (this.data[this.data.length - 1] !== this.oldTheData) {

      this.changeDetector.detectChanges();

    }

  }

}

ngAfterContentInit

ngAfterContentInit fires after the component’s content DOM initializes (loads for the first time). Waiting on @ContentChild(ren) queries is the hook’s primary use-case.

@ContentChild(ren) queries yield element references for the content DOM. As such, they are not available until after the content DOM loads. Hence why ngAfterContentInit and its counterpart ngAfterContentChecked are used.


import { Component, ContentChild, AfterContentInit, ElementRef, Renderer2 } from '@angular/core';


@Component({

  selector: 'app-c',

  template: `

  <p>I am C.</p>

  <p>Hello World!</p>

  `

})

export class CComponent { }


@Component({

  selector: 'app-b',

  template: `

  <p>I am B.</p>

  <ng-content></ng-content>

  `

})

export class BComponent implements AfterContentInit {

  @ContentChild("BHeader", { read: ElementRef }) hRef: ElementRef;

  @ContentChild(CComponent, { read: ElementRef }) cRef: ElementRef;


  constructor(private renderer: Renderer2) { }


  ngAfterContentInit() {

    this.renderer.setStyle(this.hRef.nativeElement, 'background-color', 'yellow')


    this.renderer.setStyle(this.cRef.nativeElement.children.item(0), 'background-color', 'pink');

    this.renderer.setStyle(this.cRef.nativeElement.children.item(1), 'background-color', 'red');

  }

}


@Component({

  selector: 'app-a',

  template: `

  <h1>ngAfterContentInit Example</h1>

  <p>I am A.</p>

  <app-b>

    <h3 #BHeader>BComponent Content DOM</h3>

    <app-c></app-c>

  </app-b>

  `

})

export class AComponent { }

ngAfterContentChecked
ngAfterContentChecked fires after every cycle of change detection targeting the content DOM. This lets developers facilitate how the content DOM reacts to change detection. ngAfterContentChecked can fire frequently and cause performance issues if poorly implemented.
ngAfterContentChecked fires during a component’s initialization stages too. It comes right after ngAfterContentInit.

import { Component, ContentChild, AfterContentChecked, ElementRef, Renderer2 } from '@angular/core';

@Component({
  selector: 'app-c',
  template: `
  <p>I am C.</p>
  <p>Hello World!</p>
  `
})
export class CComponent { }

@Component({
  selector: 'app-b',
  template: `
  <p>I am B.</p>
  <button (click)="$event">CLICK</button>
  <ng-content></ng-content>
  `
})
export class BComponent implements AfterContentChecked {
  @ContentChild("BHeader", { read: ElementRef }) hRef: ElementRef;
  @ContentChild(CComponent, { read: ElementRef }) cRef: ElementRef;

  constructor(private renderer: Renderer2) { }

  randomRGB(): string {
    return `rgb(${Math.floor(Math.random() * 256)},
    ${Math.floor(Math.random() * 256)},
    ${Math.floor(Math.random() * 256)})`;
  }

  ngAfterContentChecked() {
    this.renderer.setStyle(this.hRef.nativeElement, 'background-color', this.randomRGB());
    this.renderer.setStyle(this.cRef.nativeElement.children.item(0), 'background-color', this.randomRGB());
    this.renderer.setStyle(this.cRef.nativeElement.children.item(1), 'background-color', this.randomRGB());
  }
}

@Component({
  selector: 'app-a',
  template: `
  <h1>ngAfterContentChecked Example</h1>
  <p>I am A.</p>
  <app-b>
    <h3 #BHeader>BComponent Content DOM</h3>
    <app-c></app-c>
  </app-b>
  `
})
export class AComponent { }

ngAfterViewInit
ngAfterViewInit fires once after the view DOM finishes initializing. The view always loads right after the content. ngAfterViewInit waits on @ViewChild(ren) queries to resolve. These elements are queried from within the same view of the component.

In the example below, BComponent’s h3 header is queried. ngAfterViewInit executes as soon as the query’s results are available.

import { Component, ViewChild, AfterViewInit, ElementRef, Renderer2 } from '@angular/core';

@Component({
  selector: 'app-c',
  template: `
  <p>I am C.</p>
  <p>Hello World!</p>
  `
})
export class CComponent { }

@Component({
  selector: 'app-b',
  template: `
  <p #BStatement>I am B.</p>
  <ng-content></ng-content>
  `
})
export class BComponent implements AfterViewInit {
  @ViewChild("BStatement", { read: ElementRef }) pStmt: ElementRef;

  constructor(private renderer: Renderer2) { }

  ngAfterViewInit() {
    this.renderer.setStyle(this.pStmt.nativeElement, 'background-color', 'yellow');
  }
}

@Component({
  selector: 'app-a',
  template: `
  <h1>ngAfterViewInit Example</h1>
  <p>I am A.</p>
  <app-b>
    <h3>BComponent Content DOM</h3>
    <app-c></app-c>
  </app-b>
  `
})
export class AComponent { }

ngAfterViewInit
ngAfterViewInit fires once after the view DOM finishes initializing. The view always loads right after the content. ngAfterViewInit waits on @ViewChild(ren) queries to resolve. These elements are queried from within the same view of the component.

In the example below, BComponent’s h3 header is queried. ngAfterViewInit executes as soon as the query’s results are available.

import { Component, ViewChild, AfterViewInit, ElementRef, Renderer2 } from '@angular/core';

@Component({
  selector: 'app-c',
  template: `
  <p>I am C.</p>
  <p>Hello World!</p>
  `
})
export class CComponent { }

@Component({
  selector: 'app-b',
  template: `
  <p #BStatement>I am B.</p>
  <ng-content></ng-content>
  `
})
export class BComponent implements AfterViewInit {
  @ViewChild("BStatement", { read: ElementRef }) pStmt: ElementRef;

  constructor(private renderer: Renderer2) { }

  ngAfterViewInit() {
    this.renderer.setStyle(this.pStmt.nativeElement, 'background-color', 'yellow');
  }
}

@Component({
  selector: 'app-a',
  template: `
  <h1>ngAfterViewInit Example</h1>
  <p>I am A.</p>
  <app-b>
    <h3>BComponent Content DOM</h3>
    <app-c></app-c>
  </app-b>
  `
})
export class AComponent { }

ngOnDestroy
ngOnDestroy fires upon a component’s removal from the view and subsequent DOM. This hook provides a chance to clean up any loose ends before a component’s deletion.

import { Directive, Component, OnDestroy } from '@angular/core';

@Directive({
  selector: '[appDestroyListener]'
})
export class DestroyListenerDirective implements OnDestroy {
  ngOnDestroy() {
    console.log("Goodbye World!");
  }
}

@Component({
  selector: 'app-example',
  template: `
  <h1>ngOnDestroy Example</h1>
  <button (click)="toggleDestroy()">TOGGLE DESTROY</button>
  <p appDestroyListener *ngIf="destroy">I can be destroyed!</p>
  `
})
export class ExampleComponent {
  destroy: boolean = true;

  toggleDestroy() {
    this.destroy = !this.destroy;
  }
}

Tuesday, 23 February 2021

Finding the index of the string given without using any keywords

 Coding

 static void Main(string[] args)

        {

            string mainstring = "Generate reports for order wise and Product wise sales order reports";

            string secondstring = "e";

            GetPositions(mainstring, secondstring);

            string secondString = "e";

            Console.WriteLine(string.Join(", ", GetPositions(mainstring, secondString)));

            secondString = " ";

            Console.WriteLine(string.Join(", ", GetPositions(mainstring, secondString)));

            secondString = "wise";

            Console.WriteLine(string.Join(", ", GetPositions(mainstring, secondString)));

        }


Method GetPosition for the find the index


        public static int[] GetPositions(string mainstring, string secondString)

        { 

            List<int> listofsecondstring = new List<int>();

// find the single char index

            for (int i = 0; i < mainstring.Length; i++)

{

if(mainstring.Contains(secondString))

             {

                 if (secondString.Contains(mainstring[i]) && secondString == mainstring[i].ToString())

                 {

                     listofsecondstring.Add(i);

                 }

             }

                        

}

// To find the word index

            var words = mainstring.Split(' ');

            int z = 0;

            for (int J = 0; J < words.Length; J++)

            {

                z += words[J].Length ;

                if (secondString == words[J].ToString())

                {

                    listofsecondstring.Add(z);

                }

            }


            return  listofsecondstring.ToArray();

        }


        








Friday, 5 February 2021

Difference Between Dictionary And Hashtable In C#

 


Dictionary 
1.Dictionary is generic type Dictionary<TKey,TValue>
2.Dictionary class is a strong type < TKey,TValue > Hence, you must specify the data types for key and value.
3.There is no need of boxing/unboxing.
4.When you try to access non existing key dictionary, it gives runtime error.
5.Dictionary maintains an order of the stored values.
6.There is no need of boxing/unboxing, so it is faster than Hashtable.

"e.g Dictionary<string, string> EmployeeList = new Dictionary<string, string>();  
EmployeeList.Add(""Mahesh Chand"", ""Programmer"");  
EmployeeList.Add(""Praveen Kumar"", ""Project Manager"");  
EmployeeList.Add(""Raj Kumar"", ""Architect"");  
EmployeeList.Add(""Nipun Tomar"", ""Asst. Project Manager"");  
EmployeeList.Add(""Dinesh Beniwal"", ""Manager"");  "

Hashtable 
1.Hashtable is non-generic type.
2.Hashtable is a weakly typed data structure, so you can add keys and values of any object type.
3.Values need to have boxing/unboxing.
4.When you try to access non existing key Hashtable, it gives null values.
5.Hashtable never maintains an order of the stored values.
6.Hashtable needs boxing/unboxing, so it is slower than Dictionary.

"e.g Hashtable HT = new Hashtable();    
HT.Add(1,""s"");    
HT.Add(3, ""n"");    
HT.Add(4, ""j"");    
HT.Add(2, ""a"");    
HT.Add(5, ""u"");  "