Monday, 8 July 2013

Create Window Service in C#.Net

Introduction:
  • Here I am going to explain the concept of window service, what is use of window services 

Description:

  • For my project I have requirement to set up services which will be update records database table every 10 second so for that I have two option in my mind either I will create exe and create schedule task or I can create window services which has been reside in services when computer booted it will automatically run every 10 second.

  • Window service is best option for above scenario to run every 10 second. 

  • Window Service is run automatically when computer is booted. It should be control from service control manager where they can manage start, stop and paused when needed.

  • Start --> Open VS --> Files --> New Projects --> Select Window Services



  • Project contains “Service1.cs” and “Program.cs” files

  • In “Service1.cs” code contain the following event which can manage application Start, stop and paused when needed.


  •  Now Go to Service1.cs Design page --> Right Click and “Add Installer”



  • Right click on “Service process installer1” and go to “Property” and Select Account “Local System”.



  • After right click on “Serviceinstaller1” and go to property and select “Start Type” as Automatic. More about “Service Installer Process” you will get from following link
  •  


  • After configure above setting we need to write code to run window service as specific time interval.

Program.cs contain following code


  • Service1.cs file overrides two method “Start” and “Stop”. I am going write code in this two method
  • Writing code for Start Method
  • For Above code in start method I have set interval which is run every 10 second. Now for installing window services

·         Setup  project refer following link




Installation of Window service


  •  We can install window service using “InstallUtil.exe” files

  •  To install or uninstall windows service (which was created using .NET Framework) use utility InstallUtil.exe. This tool can be found in the following path (use appropriate framework version number).

·         C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe
    
  • For Uninstall Window Service  "
    C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u "Path of EXE File"
  • After installing window services. Your window is started and you can view output in log file which is reside in “d:\Service.txt” file.



Add Installer in Window Services Set up Project


  • Right Click on Solution Explorer àAdd à New Project



  • Select  Setup Project from Visual Studio Installer

  • To tell your installer that you want the output (executable and support files) from your Windows Service project to be installed on the hard-drive, right-click on Application Folder and select Add > Project Output...

  • Select Project Output file as per below figure






  • select "Primary output" from the selection box. As you can see from the Description, this is the "DLL or EXE built by the project", but will also include the support files for that executable. Click Ok when you're done.







  • At this point we have our Setup project that will generate an installer that adds our project's files to a folder on our computer. However, the installer won't actually install the service... yet.

 Connect Service Installer in your set up project


  • Select Setup project and click on Custom Action Editor. Right click on Install Folder and click on Application folder with primary output.



Build and Install Services


·         When you're ready to test your installer, go to Build > Configuration Manager and check the Build checkbox next to your Setup Project. Then when you build it will compile as well




Wednesday, 20 March 2013

404.2 The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server


By default IIS 7 does not allow ISAPI and CGI through .Net 4.0. So if you are working with older code on IIS 7 you will need to follow these steps to resolve the issue:

      
     Open IIS and click the server name



2. Double click “ISAPI and CGI Restrictions”

3. Right click ASP.NET v4.0.30319 and select “allow”





1.     Go to Start > All Programs > Administrative Tools > Services.

2.     In the services list, right-click World Wide Web Publishing Service, and then   click Stop (to stop the service), Start (to start it after it has been stopped), or Restart (to restart the service when it is running).


Tuesday, 19 March 2013

Set html body background image using asp.net


Introduction:
I have scenario that I have admin setting for back ground. Whatever image I uploaded. I want to set dynamic background image on body of form.

Approach
 My approach is to get image from database with absolute path and assign style of body tag of form

<body runat="server" id="BodyTag">

In your code behind, do this (C#):
I have data table with uploaded image name. I have made absolute path available for image.
String myImagePath = "../Images/Upload/"+dt_Background.Rows [0]["ImageURL"].ToString ();

BodyTag.Style.Add ("background-image", "url("+myImagePath+")");




 In body tag with image path “~/Images” is not working.









Friday, 8 March 2013

How to call a Master page event handler from Content page event handler?


Introduction:

I have a master page where in I have a login panel. After registration user directly logged in into site and it will go to index page with welcome screen. So I need call master page button login event.
I have researched and there seem to be I need to call the Master page button click event handler from the content page.
In your content page, Please register master page control.
<%@ MasterType VirtualPath="~/masters/Admin.master"" %>

In you content page, add line of code to fire master page button click event.
this.Master.Btn_Login_Click(sender, e);

Registration Page:
I have user registration panel, when I register and click on “Register” button. It will redirect to index page from there I send two parameter in index page first(user name, password).
Parameter value I have sent in master page user text box and password textbox. After redirect index page with user name and password set and call login click event in master page.
  if((Request.QueryString["username"] != null
&& Request.QueryString["password"] != null))
            {
               
                TextBox txtUser = (TextBox)Master.FindControl("txt_username_emailID");
                TextBox txtPass = (TextBox)Master.FindControl("txt_password");
                txtUser.Text = Request.QueryString["username "].ToString();
                txtPass.Text = Request.QueryString["password "].ToString();              
 this.Master.btn_login_Click(sender, e);

            }



Wednesday, 13 February 2013

Import Excel Data into SQL Temp table




·      Sometimes it is necessary to import data from Excel file and insert into SQL Server table or building an automated script for inserting data into SQL Server table or building an automated script for inserting data and save into another excel file to insert later. Today I will discuss all the possibilities to solve this kind of problems. You need to follow one or more than one steps of the following depending on your requirement.

·       I have following excel sheet and I want to import all data into temp SQL table.


·         SQL Server OpenRowSet command can do data transformation easily. You can do that with following simple query in SQL.

·         I created simple temp table in SQL and import all rows from excel sheet into temp table.

INSERT  INTO [dbo].[#tblImport]

SELECT  Code, BasicSalary, GrossSalary

FROM   OPENROWSET('Microsoft.ACE.OLEDB.12.0',

            'Excel12.0;HDR=YES;Database=D:\Salary_Register_offshore.xlsx',

                          'SELECT * FROM [Sheet1$]');





Output