Sunday, March 31, 2013

[ C# ] - Deploying Application with One-Click Install

1. Introduction

In this article, we will explore "Publishing the C-Sharp application" in the network path. The article shows the demonstration steps using Visual Studio 2005 IDE.

Publishing the application to a shared location benefits in such a way that the interested consumers go to that location and starts installing the published application from that network location. There is no need of cutting a CD for each client and shipping it to them.





In the above screen shot, you can see the application developers create an install-able setup and publish that in a shared location. This shared location can be:


  1. Web Location
  2. A Ftp Site
  3. Network share

Multiple clients who have access to the shared location takes the application from the shared location and installs that on their local machine. This happens for the first time. The next time onward  publisher of the application publishes the application updates on the same location. When the client starts their application they get the notification about the available update on the published location.

Publishing the application in a shared location is useful and gives benefit for both publisher and clients subscribed to the published application. In the publisher point of view, they no need to cut CDs and ship to every client. In client point of view, they will automatically get the update to the application that they are using.

In this article, we are going to look,


  1. Publishing the application in Network shared path
  2. Providing the Update notification to the Client
  3. Pushing the updates forcibly to the Installed client location

I am not providing any sample application with this article. You can try this article with any simple c# application on your own.

2. Publishing a C# application

To start the publishing wizard, open the project in visual studio, then right-click on the project name is the solution explorer. From the displayed context menu, select the publish… Option as shown below:



Then from the displayed wizard, publish the application on a network share. Once the application is published on a network share, the shared location contains a file called publish.htm by default.  Clients can access the file in the Internet explorer using the file protocol as specified below:


When a user accesses the application in Internet explorer the page looks like the below shown:


1: User navigating to publish location
2: After reviewing the content, they install the application on their local machine by clicking Install

In the video below, I explained the process of publishing the application on the network path, then installing the application by navigating to the published page on the network path.

Video 1: Explains how do you publish the application.



3. Publishing the Updates to the Application

Let us say 7 people installed the application from the network published location. Also, say the application is modified with lots of look and appearance changes and published in the same location.

In the above situation, the software update is placed on the published location. When those seven clients start their installed application next time, they get the notification that talks about getting the software update from the published location. This notification provides the end user a choice of updating the application to have the new version installed or skipping the update to use whatever version they have right now.

Click the updates button in the publish property page of the project settings. From the displayed dialog you can specify how you want to provide an update to the end user.

Providing the software updates to all the installed location via the published location is shown in the below video.

Video 2: Explains how user gets the Update from the Published location



Sometimes, we may need to push the important updates to the end locations not providing the end user the option of skipping this update. The Updates dialog has an option for that also. Use the third option shown in the below picture to push the major updates to installed subscribers.


Pushing the update to end-user is shown in the below video.

Video 3: Pushing the Major updates to end user machine.



No Sample application required for this article.

Tuesday, March 19, 2013

SQL 2005 - Understanding Auto Commit, Implicit, Explicit Transaction

1. Introduction


In SQL Server we call a complete set of action as "Transaction". The complete set of action together forms a meaningful change to the database. Say for Example, you are transferring money from one bank account to another one. This involves the below specified actions:

  1. Create a withdraw entry in the First back account
  2. Reduce the Balance in your First Bank account
  3. Create a Deposit entry in the second bank entry
  4. Increment the Balance in the Second bank account.

Now consider replacing first bank account as Savings Account and second bank account as Current Account within the same bank (For easy understanding). Now we say the bank website will consider all these four actions together as Money Transfer Transaction. Even a single action fails the entire transaction get cancelled to avoid existence of unmatched records for the account holder.

In this article, we will explore how do we use transactions in SQL server.

In SQL Server, we can set three different types of transactions. These transactions are:

  1. Auto Commit Transactions
  2. Implicit Transactions
  3. Explicit Transactions

2. Auto Commit Transactions


In "Auto Commit Transaction" mode, SQL Server immediately commits the change after executing the statement. That’s why we call this as Auto commit transaction. Auto commit transaction is the default transaction mode in SQL Server.

Have a look at the video 1.


In the video you can observe that we executed three update statements by highlighting each update statement one by one. The second update statement is made wrong intentionally. After executing all three statements, the select query result shows first and third update persisted to the database. Here, after executing each statement, the change is committed to the database automatically. That is why we call this as Auto Commit Transaction, which is the default transaction mode in SQL server 2005.

Now, have a look at the video 2.


The script is same. We have same three update statements with a same spelling mistake in the second update statement. However, this time we update all three update statements at once, by selecting all three and hitting the execute button. Here, all these three statements are executed on a single go and hence we call these three statements as batch. Since the second update statement in the batch is failed, none of the row update takes place in the database. In this case, a Roll-back is performed because of the batch failure (2nd update).

The "Go" statement actually forms the batch of SQL statement. We can write the script for video 1 and video 2 (The way the update statements are executed) as shown below:

--Example 1
--Batch 1
Update Authors Set Au_LName = 'Black' where Au_Id = '172-32-1176';
Go
--Batch 2
Update Authors Ser Au_LName = 'Voyer' where Au_Id = '213-46-8915';
Go
--Batch 3
Update Authors Set Au_LName = 'Peterson' where Au_Id = '238-95-7766';
Go
--Batch 4
Select Au_Id, Au_Fname + ',' + Au_Lname as Name,
Phone, City from Authors where Au_id in ('172-32-1176',
        '213-46-8915','238-95-7766');
Go
--Batch 5
Update Authors Set Au_LName = 'White' where Au_Id = '172-32-1176';
Update Authors Set Au_LName = 'Green' where Au_Id = '213-46-8915';
Update Authors Set Au_LName = 'Carson' where Au_Id = '238-95-7766';
Go
--Example 2
--Treat All three as a Single Batch.
--Batch 1
Update Authors Set Au_LName = 'Black' where Au_Id = '172-32-1176';
Update Authors Ser Au_LName = 'Voyer' where Au_Id = '213-46-8915';
Update Authors Set Au_LName = 'Peterson' where Au_Id = '238-95-7766';
Go
--Batch 2
Select Au_Id, Au_Fname + ',' + Au_Lname as Name,
Phone, City from Authors where Au_id in ('172-32-1176',
        '213-46-8915','238-95-7766');
Go
--Batch 3
Update Authors Set Au_LName = 'White' where Au_Id = '172-32-1176';
Update Authors Set Au_LName = 'Green' where Au_Id = '213-46-8915';
Update Authors Set Au_LName = 'Carson' where Au_Id = '238-95-7766';
Go

From the above statements, I can say that SQL server performs a Commit or Rollback after the Go statement is encountered. Hence, in the Auto commit transaction we don’t have to control the commit and rollback as it is taken care by the SQL Server. 

Also note that the roll-back will not be applied when SQL Server encounters a Runtime error. Imagine that in example 2, instead of the misspelled keyword Set we violate a constraint (Say Primary key constraint) in the second statement. In this case, SQL server commits first and third statement.

3. Implicit Transactions

In "Implicit Transaction" Mode, we can control the "Rollback" and the "Commit" operation. A new transaction automatically begins after the commit/Rollback. We can turn ON and Turn OFF this transaction mode based on the need.
In the above picture, two commit and one rollback statement are executed after turning on the Implicit Transaction. As already told after a commit or rollback a new transaction gets started and that is how you see three transactions in the above depiction. Once you turn OFF the implicit transaction, the transaction will be set back to the default Auto Commit transaction.

Use the below SQL statements to Turn-ON or Turn-OFF the implicit transaction:

Set Implicit_Transactions On;
Set Implicit_Transactions Off;

Now Have a look at the below given example:

--Example 3
Set Implicit_Transactions On;

--Transaction 1
Update Authors Set Au_LName = 'Black' where Au_Id = '172-32-1176';
commit;

--Transaction 2
Update Authors Set Au_LName = 'Voyer' where Au_Id = '213-46-8915';
Rollback;

--Transaction 3
Update Authors Set Au_LName = 'Peterson' where Au_Id = '238-95-7766';
commit;

--Select the Authors, to see the effect of Update statement
Select Au_Id, Au_Fname + ',' + Au_Lname as Name,
Phone, City from Authors where Au_id in ('172-32-1176',
        '213-46-8915','238-95-7766');

--Revert back to Original
Update Authors Set Au_LName = 'White' where Au_Id = '172-32-1176';
Update Authors Set Au_LName = 'Green' where Au_Id = '213-46-8915';
Update Authors Set Au_LName = 'Carson' where Au_Id = '238-95-7766';
Go

Set Implicit_Transactions Off;

In this example, three update statements are executed followed by either a Rollback or a commit. Note that, here we have the Freedom of specifying where we want to do a commit or a roll back which we cannot do in the default Auto Commit Transaction mode. Also be aware that even though we can specify where we want to end the transaction, it is not possible to control the starting of the transaction. Because, the transaction starts immediately after it ends.

After the first Update statement, we asked for a commit, so the update of author’s last name as Black is committed to the database and new transaction is started after that. The second Transaction comes to an End when the rollback statement is executed. The update of author’s last name (as Voyer) kept in the memory is discarded after the rollback and one more new Transaction is started. Note that at the end of the script we turned off the implicit transaction. Once the implicit transaction is turned off, the mode changes to default Auto Commit transaction mode.

OK. What is the Final result of executing this script? The first and last update statement committed to the database and the second update is discarded.

4. Explicit Transactions


In "Explicit Transaction", we control the starting and ending of the transaction. Look at the below Pic:
As we control the starting and ending of the transaction, this transaction type is widely used while making the compiled unit of code Say for Ex. Stored Procedures. Have a look at the below specified code:


Here we started two transactions and note that the transaction will end when there is a commit or rollback. In this example, the first transaction will get succeed and the “Data Inserted message” will appear in the message area while executing the script. The second Transaction fails as Not Null column Contract is skipped from the insert (Note skipping a column means we like to leave the column as null) statement.


As this is a constraint violation and this happens at runtime the "@@Error" in our script returns a Non-Zero Error number. When the statement execution succeeds without any problem, we do get zero in the "@@Error environment variable". The remaining portion of the script is understandable. If you need complete example, you can take that from below:

--Example 04
Declare @ErrNo int
Begin Transaction;
Insert into Authors(Au_id, au_lname, au_fname, Phone, contract)
values ('112-33-1811', 'Jane', 'Marey', '409 210-2331', 1);
Set @ErrNo = @@Error;
if @ErrNo != 0
        Begin
               Print 'Error Occurred. Transaction Cancelled';
               RollBack;
        End
else
        Begin
               Print 'Data Inserted.';
               Commit;
        End
Begin Transaction;
Insert into Authors(Au_id, au_lname, au_fname, Phone)
values ('112-33-1234', 'Billy', 'Jones', '409 234-2232');
Set @ErrNo = @@Error;
if @ErrNo != 0
        Begin
               Print 'Error Occurred. Transaction Cancelled';
               RollBack;
        End
else
        Begin
               Print 'Data Inserted.';
               Commit;
        End
-- Revert back the Change
Delete from Authors where Au_Id = '112-33-1811';

Thursday, March 07, 2013

[ MFC ] - Setting Image Background for dialog


1. Introduction




In this article, we are going to see how do we set the background image for the dialog based application. You can use the same technique specified here for any dialog by handling the WM_PAINT message.

The example for this article is shown below:




2. Add Bitmap Resource


First, we should create a bitmap resource. Once the resource is ready we can start adding the code to display the background image for the dialog. Follow the below-provided steps:

1) Create a Dialog based MFC Application and name it as DialogBackground.
2) Run the wizard created default application
3) Take screen shot of the dialog and paste it to the paint brush
4) Draw something manually inside the dialog display area.
5) Then copy the image from the display area and create a bitmap resource from it. This fifth step is shown in the below video

Once the resource is ready, we can display the image as a background for the dialog box.

3. About the source code

Modify the OnPaint() handler as shown below:
void CDialogBackgroundDlg::OnPaint()
{
                  if (IsIconic())
                  {
                                   CPaintDC dc(this); // device context for painting
                                   SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
                                   // Center icon in client rectangle
                                   int cxIcon = GetSystemMetrics(SM_CXICON);
                                   int cyIcon = GetSystemMetrics(SM_CYICON);
                                   CRect rect;
                                   GetClientRect(&rect);
                                   int x = (rect.Width() - cxIcon + 1) / 2;
                                   int y = (rect.Height() - cyIcon + 1) / 2;
                                   // Draw the icon
                                   dc.DrawIcon(x, y, m_hIcon);
                  }
                  else
                  {
                                   //Sample 01: Required Declarations
                                   CDC MemDC ;
                                   CBitmap bmp ;
                                   CPaintDC dc(this);
                                  
                                   //Sample 02: Get the Client co-ordinates
                                   CRect rct;
                                   this->GetClientRect(&rct);
                                   //Sample 03: Create the Dialog Compatible DC in the memory,
                                   //                                                                                                      Then load the bitmap to the memory.
                                   MemDC.CreateCompatibleDC ( &dc ) ;
                                   bmp.LoadBitmap ( IDB_BITMAP_BKGR ) ;
                                   MemDC.SelectObject ( &bmp ) ;
                                   //Sample 04: Finally perform Bit Block Transfer (Blt) from memory dc to
                                   //                                                                                                     dialog surface.
                                   dc.BitBlt ( 0, 0, rct.Width() , rct.Height() , &MemDC, 0, 0, SRCCOPY ) ;
                                   CDialog::OnPaint();
                  }
}
Explanation:
Sample 01: Required declarations are added.

Sample 02: The client coordinate of the displayed dialog is retrieved and stored in a CRect instance rct.  Note this client rectangle returned will match the dimension of the bitmap resource that we created earlier.

Sample 03: The method call CreateCompatibleDC will create the memory device context which compatible to the dialog. That is it takes the settings (Pen, Brush etc) from the device context of the dialog.  Once the Memory device context is created, we load the bitmap from the resource into CBimap object bmp. Finally, we load this bitmap to the memory device context MemDC.

Sample 04: BitBlt is called on the CPaintDC.  BitBlt stands for Bit Block Transfer. Here we are transferring the bitmap content prepared in the memory (MemDC) to the destination CDialog.

Look at the below video to see the output:


Source Code: Download
Like this site? Tell it to your Firend :)