Monday, September 12, 2016

Maintaining ConnectionSetting and AppSetting in separate files in Visual Studio 2013 application


In this tutorial, i will try to separate database connection settings details and application related settings into separate files than keeping it in Web.config file. To do this, first thing i will do is to open the web application in which i want to create separate files


1. Select the project 
2. Right click on project and select "Add -> Add new item"



3. select "Visual C# -> Web Configuration File" option
4. enter the file name as "ConnectionSettings.config" or file name of your choice. and click on "Add" button



5. Enter the database connection setting details into "ConnectionSettings.config" file as shown below
<?xml version="1.0"?> 
<connectionStrings>
 <add name="db" connectionString="Data Source=VAIO\\MSSQLSERVER2012;User ID=sa;    Password=password1; Initial Catalog=AdventureWorks2008R2;
   Persist Security Info=true;"/>
</connectionStrings>


6. Now we will follow the same steps as followed for "ConnectionSettings.config" to add "Settings.config" file



7. enter the following configuration details in "Settings.config" file
<?xml version="1.0"?> 
<appSettings>
 <add key="welcomeMsg" value="You are welcome to simplyDotnet"/>
</appSettings>

8. Now we need to complete the final step to add the reference to newly added "ConnectionSettings.config" and "Settings.config" file in "Web.config" file:
<connectionStrings configSource="ConnectionSettings.config"></connectionStrings> 
<appSettings configSource="settings.config"/>




9. Add a reference to following namespace in your *.cs file

using System.Configuration;

10. Now we can use following code to read connectionsettings details:

ConfigurationManager.ConnectionStrings["dbConnection"].ToString();

11. We need to use the following code to read appsettings details

 ConfigurationManager.AppSettings["welcomeMsg"].ToString();


  Hope this helps

Sunday, September 11, 2016

Getting started with Windows Service in Visual Studio 2013


Download the tutorial code here: helloworldservice.zip 

Lets start with building a simple windows service in Visual Studio which will write "Hello World" text with current Date and Time stamp into a text file.
  1. Open Visual Studio
  2. Navigate to "File -> New -> Project" menu option

  1. Navigate to "Visual C# -> Windows Desktop" under installed templates options
  2. Select "Windows Service"
  3. Enter "Project Name" and "Location" as shown below

  1. Rename the "Service.cs" file to "HelloWorldService.cs"


  1. Go to code behind file of "HelloWorldService.cs" file and enter the following code:
/// <summary> /// Create instance of timer class and set the interval. /// define the timer elapsed event
/// call the start method of the timer object /// </summary>
public HelloWorldService()
{
    InitializeComponent();
     Timer oTimer = new Timer();
     oTimer.Elapsed += oTimer_Elapsed;
     oTimer.Interval = 6000;
     oTimer.Start(); }
/// <summary> /// define the logic to write the file with "HelloWorld" text with datetime stamp /// </summary> /// <param name="sender"></param> /// <param name="e"></param>
void oTimer_Elapsed(object sender, ElapsedEventArgs e)
{
 StreamWriter oStreamWriter =   new StreamWriter("G:\\SimplyDOTNET\\ServiceOutput\\HelloWorld.txt", true);
 oStreamWriter.WriteLine("Hello World" + DateTime.Now.ToString());
 oStreamWriter.Close();
 oStreamWriter = null;
}



  • run the service
  • donot close the alert window which is shown


  1. navigate to the folder which is set as a destination to write the text file
  2. you can find a file and if you open it, you will find the text is written to the file

   
Hope this helps.

Thursday, September 1, 2016

Free .NET assembly browser and decompiler



In this article i will talk about one of the best .NET assembly browser and decompiler, which is absolutely free.

The .NET decompiler name is ILSPY which can be downloaded from http://ilspy.net/.


Once downloaded in your local folder, you will be required to extract the files of ILSpy_Master_2.4.0.1963_Binaries.ZIP file. Go the the extracted folder and click on "ILSpy.exe".































As you can see in the above screen shot, mscorlib.dll internal implementation details are decompiled and shown:















For demo purpose, i am selecting the "Open from GAC" option which will display all the dll's registered in GAC of my machine























Once, i select a dll in this case "CrystalDecisions.CrystalReports.Design" dll it will show all the details like references and resource (the code implementation).






















All the internal implementation details will be displayed. I loved the idea that this decompiler is free and source is also available for download if you are interested in enhancing the existing features.

Hope this help. Happy Coding and debugging....