Tuesday 17 April 2012

Notify Icon In WPF applications

   Notify icon has been a real serious issue for me when I migrated from windows forms to WPF. While creating Notify icon is so simple for Windows applications, it is not the same for a WPF application. Searches in net revealed the installation of some libraries, changing the Forms notify icon to suit WPF etc.. All these codes belong to the authors and we cannot simply use it for royalty free.

   So basically I figured out a simple one line code for using notify icon in WPF. And it greatly works for me. If this suit you feel free to use this technique, else just ignore this if you a genius already. So here we go.

  Create a System.Windows.Forms.NotifyIcon object and enable its visibility. This will give a notify icon we needed and that is simple. Now we should add a WPF context menu for this icon.



Adding  a WPF context menu can be much simpler too. First create the required wpf context menu in the XAML code behind as usual. And give a name to this context menu say contextmenu1. Now add the below kind of code to the notify icon's click event.



Now if we right click the notify icon the context menu of  WPF style will pop up. And if we right click again the context menu will disappear. But this would not be convenient to the users. The context menu should disappear like in usual cases. So that is why the line this.Activate();  is present in the above code. So we activate the WPF main form when the context menu opens. And when we main window gets deactivated we will close the context menu as below.

 


And hence we achieve a WPF with notify icon and context menu. Try this and let me know your comments.

Thursday 26 January 2012

Loading data to Sybase tables in C#

  If a sybase connection is to be established in C# then the AseConnection object can be used to accomplish it successfully.

  First the sybase reference should be added to the project solution by choosing Sybase.Data.AseClient component from the .Net Tab. This component doesn't comes free with .Net installation and it requires the shareware Sybase SQL client has to be installed. Once this is done, the below code can help in establishing a sybase connection.



Once the connection is established you can execute the queries by using a AseCommand object as below.



  Use the ExecuteScalar, ExecuteReader or ExecuteNonQuery appropriately as required. Finally close the connection by using connection.Close() command.



  Thats it and you may use them as per your requirement.


Example:
The below code takes a folder name from a form. The folder contains files each of which in turn contains thousands of insert statements that are to be inserted to a sybase tables. Once the user chooses the folder path and click the load command button the insert statements are fetched from each files and executed in batches of 5000. This is because in this case of  huge volume of rows to be inserted if the insertion is done one by one it would consume more time than the manual process and it also cannot be done in bulk, because once the operation fails it would be difficult to identify the rows that are missed. So it is convenient to carry out the insertion in terms of 5 thousands or lesser.

Form1.cs


Form1.Designer.cs



Make modifications to the above and use try catch block and test it for your connection string parameters. So basically this is just a simple guideline and cannot be used as such. In addition to this multi-threading can also be used if your server allows upload from multiple connections. In that case each thread opens a separate sql connections and executes rows insertions.

This example might sound to be a simple solution for the real programmers around the world. But some fools in a reputed MNC that works for a leading US company couldn't figure out this simple automation and opted to perform this tedious operations for months by torturing the innocent new joiners. While this is nothing but a simple sybase connection example.

If you have any queries and suggestion please leave your comments and more working references will be shared if necessary.

Note:
The example given here resembles a notepad when generated as a form and if this means something else to you, then it could be only you. Only you in the whole world can understand the reason why this app resembles a notepad's look and you will also know that it is me. I am sharing this code in public with a hope that one day you might come across this and realize that how much I cared for you. I had even more better automation to save you from those manual tasks. I hated to watch you work for those fools. Darling, I am more than what I did there. I thought that one day I will own you  and take you home as my princess. But I failed and lost you. I cannot take this separation from you and I will soon end my life, but not without proving myself to the world. I just wanted you to understand that I cared about you so much and I will always love you. Where ever you are be happy, because as you believe it is one way to be wise and also beautiful.