Windows service on local computer started and then stopped

I am getting below error while running windows service:
The "my service name" service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs"


It is difficult to track down this type of generic errors. You can do the following options to figure out the issue and fix it.
Option 1: With Event viewer you can see the errors and on which line number we are getting it. Do the following steps to open the Event Viewer:
1) Type Event Viewer on start and click it.
2) In popup look for “Windows Logs” under “Event Viewer (Local)” on left panel and click it.
3) Now you will get list of windows logs, select “Application” from it.
Here you can search for error, see the screen shot below:

Option 2: You can have try catch blog with logging errors to table.
I have created a small function to log error as below:

public static void getLog(string msg)
 string query = "insert into LOGTABLE (DATE,LEVEL,MESSAGE,EXCEPTION) values(current_timestamp,'INFO','"+msg+"','-')";
 SqlCommand command;
 SqlDataReader dataReader;
 var DB = dbConnect();
 command = new SqlCommand(query, DB);
 dataReader = command.ExecuteReader();

Add more try/catch blocks and logging to the app to get a better picture of what’s going on.
Hope it helps someone!!!

Installing Windows Services using command prompt

In this post, I am sharing how you can install or uninstall windows services in your local windows system [Windows 10]. Assuming you already built one windows services using C# and want to install it using the command prompt. Follow the below steps to install on your local windows 10 environment:

Step 1: Before you install a windows services you need to generate EXE file. And for generating you need to go to the Visual studio and clean and build your application, which will automatically generate exe file in bin folder.

Step 2: Open command prompt “as Administrator”

Step 3: Navigate to folder “C:\Windows\Microsoft.NET\Framework\v4.0.30319>”
command line

Step 4: For installing you need to run below command:
installutil.exe "E:\Suresh\Projects\TestWindowsService\TestWindowsService\obj\Debug\TestWindowsService.exe"

Step 5: Open Run window (Window + R), type in “services.msc”, and press enter. A “Services” popup will open.
Step 6: In “Services” popup search for your service name and start it by right click on it.
Thats All !!!
Now If you want to uninstall it, type the following command:
installutil.exe -u “E:\Suresh\Projects\TestWindowsService\TestWindowsService\obj\Debug\TestWindowsService.exe”

Hope it will help someone!!!

Could not load file or assembly System.Web.Http.WebHost

After deploying to IIS server from Visual Studio getting below error,

Could not load file or assembly 'System.Web.Http.WebHost, Version=, Culture=neutral,PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file

You can fixed by either of below steps:
Option 1: To Resolve issue you have to move “System.Web.Http.WebHost.dll” file in “bin” folder to IIS. And restart the server to reload everything again. Check the site should work.
Option 2: Follow below steps:
1) Go to “Solution Explorer” in Visula Studio.
2) Right Click on “System.Web.Http.WebHost” in “References” and select properties.
3) Update property “Local Copy” to “True”.
4) Clear and Build again to have DLL in bin folder.
5) Copy the “System.Web.Http.WebHost.dll” file in “bin” folder to IIS.
Application should work.
I am getting below errors (one after the another build) so I repeated the steps to update “Local Copy” to “True” and build and copied the DLL to IIS, it started working.

Could not load file or assembly 'System.Net.Http.Formatting, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Could not load file or assembly 'System.Web.Http, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Hope it helps someone to fix issue.

How to format cell using EPPlus package in C# MVC

EPPlus is a great package used in C# to export and import excel files. Here I am sharing how we can format data while exporting data to excel.

1) Formatting text, possible values:”Italic”, “Bold”

sheetcreate.Cells[1, 1].Style.Font.Bold = true;

2) For merging cells

sheetcreate.Cells[1, 1, 1, 5].Merge = true; //Address "A1:A5"

3) Setting up border for cells

sheetcreate.Cells[1, 1].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);

4) Setting up cell alignment. It can be “Center”, “Right” and “Left”.

 sheetcreate.Cells[1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

5) Setting up background color

 sheetcreate.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml("#e0e0e0"));

6) To avoid word wrap in excel use blow function. It should be written after creation of excel sheet.


Reading excel file using EPPlus package

Here I am sharing how you can read excel file using EPPlus package. Many solution given on internet but nothing work for me, so I thought of sharing what worked for me.
In below example method required excel file name (full server path) and then it will read first sheet of the workbook. You can modify it according to your requirement.

public void readXLS(string FilePath)
    FileInfo existingFile = new FileInfo(FilePath);
    using (ExcelPackage package = new ExcelPackage(existingFile))
        //get the first worksheet in the workbook
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        int colCount = worksheet.Dimension.End.Column;  //get Column Count
        int rowCount = worksheet.Dimension.End.Row;     //get row count
        for (int row = 1; row <= rowCount; row++)
            for (int col = 1; col <= colCount; col++)
                Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());

If you want to insert excel data into table you can refer to my blog:

how to read data from excel sheet and insert into database table in C#