Search This Blog

Saturday 23 June 2018

Trigger interview question and answer


If you have two table, Table1(Sales) and Table2(SalesOrder). Table1 containing Sales Item details and Table2 containing Sales Item Order details.
Create a trigger on Table2 and update total Sales done of each Item in Table1.


--Oracle
create table Table1
(
  Table1Id number,
  Total number
)

create table Table2
(
  Table2Id number,
  Table1Id number
)

insert into Table1 values(1,0);
insert into Table1 values(2,0);
insert into Table1 values(3,0);

select * from Table1;

CREATE OR REPLACE TRIGGER Table2_tr
AFTER INSERT ON Table2
  FOR EACH ROW
BEGIN
  update Table1 set Total = (Total + 1) where Table1Id = :new.Table1Id;
END;

insert into Table2 values(1,1);

select * from Table2;

--SQL
create table Table1
(
  Table1Id int,
  Total int
)

create table Table2
(
  Table2Id int,
  Table1Id int
)

insert into Table1 values(1,0);
insert into Table1 values(2,0);
insert into Table1 values(3,0);

select * from Table1;

CREATE TRIGGER Table2_tr ON Table2
AFTER INSERT
AS
BEGIN
  declare @Table1Id int
  select @Table1Id = Table1Id from inserted
  update Table1 set Total = (Total + 1) where Table1Id = @Table1Id;
END;

insert into Table2 values(1,1);

select * from Table2;

Sunday 17 June 2018

How to Restrict the Application to Just One Instance in C#


You can achieve it using threading.

More info on threading visit Microsoft.com

using System.Threading;


[STAThread]
static void Main()
{
    using (Mutex mutex = new Mutex(false, "TestApplication"))
    {
        if (!mutex.WaitOne(0, false))
        {
            MessageBox.Show("Instance already running");
            return;
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
        mutex.ReleaseMutex();
    }
}

OR

[STAThread]
static void Main()
{
    bool createdNew;
    using (Mutex mutex = new Mutex(false, "TestApplication", out createdNew))
    {
        if (createdNew)
        {
            MessageBox.Show("Instance already running");
            return;
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}