Thursday, November 12, 2015

What Is MVC?

MVC is a software architecture and  design patterns - the structure of the system - that separates domain/application/business (whatever you prefer) logic from the rest of the user interface. It does this by separating the application into three parts: the model, the view, and the controller.
The model manages fundamental behaviors and data of the application. It can respond to requests for information, respond to instructions to change the state of its information, and even to notify observers in event-driven systems when information changes. This could be a database, or any number of data structures or storage systems. In short, it is the data and data-management of the application.
The view effectively provides the user interface element of the application. It'll render data from the model into a form that is suitable for the user interface.
The controller receives user input and makes calls to model objects and the view to perform appropriate actions.

Trigger

Trigger:
èAfter Triggers (For Triggers)
èInstead Of Triggers
 After Triggers
These triggers run after an insert, update or delete on a table. They are not supported for views. 
AFTER TRIGGERS can be classified further into three types as:
     AFTER INSERT Trigger.
     AFTER UPDATE Trigger.
    AFTER DELETE Trigger.
CREATE TABLE Employee_Test
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)

INSERT INTO Employee_Test VALUES ('Anees',1000);
INSERT INTO Employee_Test VALUES ('Rick',1200);
INSERT INTO Employee_Test VALUES ('John',1100);
INSERT INTO Employee_Test VALUES ('Stephen',1300);
INSERT INTO Employee_Test VALUES ('Maria',1400);


AFTER INSERT TRIGGER
CREATE TABLE Employee_Test_Audit
(
Emp_ID int,
Emp_name varchar(100),
Emp_Sal decimal (10,2),
Audit_Action varchar(100),
Audit_Timestamp datetime
)

 After Insert Trigger

This trigger is fired after an INSERT on the table.
CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test]
FOR INSERT
AS
          declare @empid int;
          declare @empname varchar(100);
          declare @empsal decimal(10,2);
          declare @audit_action varchar(100);

          select @empid=i.Emp_ID from inserted i; 
          select @empname=i.Emp_Name from inserted i;
          select @empsal=i.Emp_Sal from inserted i;        
          set @audit_action='Inserted Record -- After Insert Trigger.';

          insert into Employee_Test_Audit
           (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
          values(@empid,@empname,@empsal,@audit_action,getdate());

          PRINT 'AFTER INSERT trigger fired.'
GO
To see the newly created trigger in action, lets insert a row into the main table as:
insert into Employee_Test values('Chris',1500);

Now, a record has been inserted into the Employee_Test table. The AFTER INSERT trigger attached to this table has inserted the record into the Employee_Test_Audit as:

AFTER UPDATE Trigger

This trigger is fired after an update on the table. Let’s create the trigger as:
CREATE TRIGGER trgAfterUpdate ON [dbo].[Employee_Test]
FOR UPDATE
AS
          declare @empid int;
          declare @empname varchar(100);
          declare @empsal decimal(10,2);
          declare @audit_action varchar(100);

          select @empid=i.Emp_ID from inserted i; 
          select @empname=i.Emp_Name from inserted i;
          select @empsal=i.Emp_Sal from inserted i;        
         
          if update(Emp_Name)
                   set @audit_action='Updated Record -- After Update Trigger.';
          if update(Emp_Sal)
                   set @audit_action='Updated Record -- After Update Trigger.';

          insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
          values(@empid,@empname,@empsal,@audit_action,getdate());

          PRINT 'AFTER UPDATE Trigger fired.'
GO

AFTER UPDATE
update Employee_Test set Emp_Sal=1550 where Emp_ID=6
This inserts the row into the audit table as:

After Update Trigger.   
(c) AFTER DELETE Trigger
This trigger is fired after a delete on the table

CREATE TRIGGER trgAfterDelete ON [dbo].[Employee_Test]
AFTER DELETE
AS
          declare @empid int;
          declare @empname varchar(100);
          declare @empsal decimal(10,2);
          declare @audit_action varchar(100);

          select @empid=d.Emp_ID from deleted d;
          select @empname=d.Emp_Name from deleted d;         
          select @empsal=d.Emp_Sal from deleted d;       
          set @audit_action='Deleted -- After Delete Trigger.';

          insert into Employee_Test_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
          values(@empid,@empname,@empsal,@audit_action,getdate());

          PRINT 'AFTER DELETE TRIGGER fired.'
GO

-- After Delete Trigger.  All the triggers can be enabled/disabled on the table using the statement

ALTER TABLE Employee_Test {ENABLE|DISBALE} TRIGGER ALL
Specific Triggers can be enabled or disabled as:

ALTER TABLE Employee_Test DISABLE TRIGGER trgAfterDelete
This disables the After Delete Trigger named trgAfterDelete on the specified table.
è Instead Of Triggers
INSTEAD OF TRIGGERS can be classified further into three types as:
=>      INSTEAD OF INSERT Trigger.
=>       INSTEAD OF UPDATE Trigger.
=>       INSTEAD OF DELETE Trigger.
Lets create an Instead Of Delete Trigger as:

CREATE TRIGGER trgInsteadOfDelete ON [dbo].[Employee_Test]
INSTEAD OF DELETE
AS
          declare @emp_id int;
          declare @emp_name varchar(100);
          declare @emp_sal int;
         
          select @emp_id=d.Emp_ID from deleted d;
          select @emp_name=d.Emp_Name from deleted d;
          select @emp_sal=d.Emp_Sal from deleted d;

          BEGIN
                   if(@emp_sal>1200)
                   begin
                             RAISERROR('Cannot delete where salary > 1200',16,1);
                             ROLLBACK;
                   end
                   else
                   begin
                             delete from Employee_Test where Emp_ID=@emp_id;
                             COMMIT;
                             insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
                             values(@emp_id,@emp_name,@emp_sal,'Deleted -- Instead Of Delete Trigger.',getdate());
                             PRINT 'Record Deleted -- Instead Of Delete Trigger.'
                   end
          END
GO




Sunday, November 1, 2015

Compare two SQL Server Databases


--     Table Difference tool allows you to discover and reconcile differences between a source and destination table or a view. Tablediff Utility can report differences on schema and data. The most popular feature of tablediff is the fact that it can generate a script that you can run on the destination that will reconcile differences between the tables. TableDiff.exe takes 2 sets of input;
--     Connectivity - Provide source and destination objects and connectivity information. • Table Difference tool allows you to discover and reconcile differences between a source and destination table or a view. Tablediff Utility can report differences on schema and data. The most popular feature of tablediff is the fact that it can generate a script that you can run on the destination that will reconcile differences between the tables. dbcompare.exe takes 2 sets of input;
--     Connectivity - Provide source and destination objects and connectivity information. • Table Difference tool allows you to discover and reconcile differences between a source and destination table or a view. Tablediff Utility can report differences on schema and data. The most popular feature of tablediff is the fact that it can generate a script that you can run on the destination that will reconcile differences between the tables. dbcompare.exe takes 2 sets of input;
--Connectivity - Provide source and destination objects and connectivity information.

Db compare exe Download

Wednesday, October 21, 2015

LINQPad USE


LINQPad allows you to execute single LINQ commands against an existing EF model, or even to write dot net code in the editor and execute it like a little dynamic dot net environment.  The main application is free, but there is an auto-complete feature to the editor that you must pay in order to activate. Believe me it is worth it to pay for the license, you also support the author and show him the application is worthMONEY .  The license is very inexpensive and well worth the price in order to get intellisense like behavior on your LINQ queries.
The first couple versions I played with worked great with SQL Server, but could not load the VistaDB EF provider.  I was bummed, but at least I could debug my queries against SQL Server, and then run against VistaDB to make sure it worked the same way.  But I recently got an email from the author of LINQPad that it could load VistaDB, in version 2.


Monday, October 19, 2015

Email Templates

Sample
Main program
static void Main(string[] args)
        {
            new Email().Run();
        }


Cs.file

public class Email
    {
       public void Run()
       {
           try
           {
               System.Net.Mail.MailMessage newmsg = new System.Net.Mail.MailMessage();

               newmsg.From = new MailAddress("from address full");

               newmsg.Subject = "Testing2";
               newmsg.To.Add("to address");
               //newmsg.To.Add("choulla.naresh@gmail.com");
               newmsg.Body = "Hellooooo2";
               SmtpClient smtp = new SmtpClient();
               smtp.Host = "host address";
//port number
               smtp.Port = "port number";
               smtp.Credentials = new System.Net.NetworkCredential("username",                      "password");
               //smtp.UseDefaultCredentials = false;
               smtp.EnableSsl = false;
               smtp.Send(newmsg);
           }
           catch(Exception Ex)
           {
               //smtp.Send(msg);
           }
       }

    }

Tuesday, October 6, 2015

Read Xml data and display Jquary

<?xml version="1.0" encoding="utf-8" ?>
        <booklist>
            <book>
                <title>Naresh</title>
                <author>Naresh1</author>
                <genre>Classic</genre>
            </book>
            <book>
                <title>abc</title>
                <author>def</author>
                <genre>Classic</genre>
            </book>
            <book>
                <title>qqq</title>
                <author>asd</author>
                <genre>www</genre>
            </book>
          
        </booklist>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
            $(document).ready(function () {
                $("#contentArea").append("<ul></ul>");
                $.ajax({
                    type: "GET",
                    url: "data.xml",
                    dataType: "xml",
                    success: function (xml) {
                        $(xml).find('Book').each(function () {
                            var sTitle = $(this).find('Title').text();
                            var sAuthor = $(this).find('Author').text();
                            var sGenre = $(this).find('Genre').text();
                            $("<li></li>").html(sTitle + ", " + sAuthor + "," + sGenre).appendTo("#contentArea ul");
                        });
                    },
                    error: function () {
                        alert("An unexpected error has occurred during processing.");
                    }
                });
            });
        </script>

     
        <div id="contentArea">

        </div>