Sys.WebForms.PageRequestManagerServerErrorException

Posted by Venkat | Labels:

When i was worked with Ajax with timer Control i have the task ie: updating the some control inside the UpdatePanel ie: I am refreshing or call the method  at regular intervals. That time i got this error

 Sys.WebForms.PageRequestManagerServerErrorException ... 404 not found

This error occurs only when the page is idle for 1 min or more than 1 min, so i googled to get the solution for this issue. there is number of solution available according the problem and at which situation you are using.

Solution: 

< asp:ScriptManager ID="ScriptManager1" runat="server"   EnablePartialRendering="false" />

Creating Windows Service using ASP.NET

Posted by Venkat | Labels: , ,

Good morning to All

Now I am going to explain how to Create a windows Service in asp.net

What is the purpose of Creating windows service in asp.net ?

For ex: if you have the task of Sending birthday email to the members Automatically or  you are going to do some manipulation on particular day or do some manipulation on Consecutive intervals so at this situation you can go for Window service or even you can do birth email process through Sql Job Scheduling.

Now see how to create a simple service.

Choose Create Project -> VisualC# -> Windows -> Windows Service

Fig 1:


So once you give the windowService Name it will show this - by default the serviceName is Service1 here i am changed to TimerService.

Fig 2:



Then Right Click on Service - Select -> Add Installer

Fig 3:


Then the ProjectInstaller window show two Services like serviceProcessIntaller1 , serviceInstaller1 so you have to change some Properties for this service

Properties of serviceProcessIntaller1

Account - > there are some 4 types of account are there as per the requirement

1) LocalService - for only particular computer

2) Network Service - to access resource on remote side ie: server

3) Local System - to works on local System

4) User - Based on User credentials

For Brief Description Check it :

http://technet.microsoft.com/en-us/library/ms143504.aspx

Fig 4:


Properties of serviceInstaller1

StartType :

1) Automatic

2) Manual

3) Disabled

You have to specify whether you are going to start this Service Manually or Automatically or Disabled the Service.then give the name for ServiceName Property , And give some Name for DisplayName property - this name will be displayed on your servicelist to identify your Service.

Fig 5:


Now we going to write the code for Service on Service1.cs

by default it has OnStart and OnStop Event - so this event occurs when service start and stop.

using System.Timers;

Add timer to do some manipulation on particular intervals

  //Initialize the timer
Timer timer = new Timer();

so here is the code - what i am doing is create a method called AddToFile where i am adding the string to the file ie: on C Directory if the file is present it will write it on  or it will create a new file with the specified name and write the content on that file.

And onStart Event i am creating the Timer Elapsed event which called at every 1 minute so their i am adding another entry on that event.

Finally onStop Event i set the timer enabled = false and made last entry ie: Service stopped.

protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            //add line to the file
            AddToFile("Make starting service");

            //ad 1: handle Elapsed event
            timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

            //ad 2: set interval to 1 minute (= 60,000 milliseconds)
            timer.Interval = 60000;

            //ad 3: enabling the timer
            timer.Enabled = true;
        }

  protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            timer.Enabled = false;
            AddToFile("Make stopping service");
        }

  private void AddToFile(string contents)
        {

            //set up a filestream
            FileStream fs = new FileStream(@"c:\timelog.txt", FileMode.OpenOrCreate, FileAccess.Write);

            //set up a streamwriter for adding text

            StreamWriter sw = new StreamWriter(fs);

            //find the end of the underlying filestream

            sw.BaseStream.Seek(0, SeekOrigin.End);

            //add the text
            sw.WriteLine(contents);
            //add the text to the underlying filestream

            sw.Flush();
            //close the writer
            sw.Close();
        }
        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            AddToFile("Make Another entry");
        }

so once you written the code just press F5 to run it show this dialog

Fig 6:

so you can start the service to start the service you have to install installutil.exe through commandline.

so go to Command Prompt on this Path.

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

From here you have to install the service , once you compile the project exe file is created on bin folder.

here is the step to install the Service

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727: InstallUtil "F:\WindowsService1\bin\Debug\WindowsService1.exe"

so if you press enter. Service will be installed.

To UnInstall The service follow this line

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\: IntsallUtil  /u"F:\WindowsService1\bin\Debug\WindowsService1.exe"

Once the Serice is installed it will be available on your Service list To check this follow this one

Start -> Control Panel -> Administrative Tools -> Services -> then find the Service as you given while creating the windowsService ie: DisplayName : TimerService

Fig7:



so if you set the Startup Type - Automatic - the service started when the System boots, you can also start the service Manually Right Click on the Service -> start. to start the service.

After the start the service check the C:\ Directory - you can check the file with the entry.

Happy Coding.


Enhanced by Zemanta

Compress and Cache the CSS and JS file using ASP.NET

Posted by Venkat | Labels: ,

HTTPCombiner
Credit goes to : http://code.msdn.microsoft.com/HttpCombiner

In my last article i have discussed how to compress .aspx pages. Now we are going to see how to compress the Javascript and CSS file.

Once the JS and CSS file is requested to the server and has been Cached. On next time You get it from Cache.If i have 10 Javascript file and 10 Stylesheets , every time when you request the page - each time it requested the server for JS and CSS file ie: 10 request to server for CSS and 10 request to Server for JS, to avoid the issue we have to combine all the CSS as one file and all JS as one file. Therefore only one request is sent to the server to get the JS file so totally two request has been sent to the server one for JS and CSS.

There is some JS and CSS compressor tools available for free, these tools will compress or minify your JS or CSS file this is the one waybut we have to do manually.

If you want to compress the file at runtime, here i am using HTTPCombiner to make the all the CSS as 1 CSS and all the JS and 1 JS file.

It has one file ie: HTTPCombiner.ashx - to Compress the CSS and JS file.Place the file on your project.
Add these two line to include all your Css and Js file on your appSettings tag on your web.config, You can get the file name using key part of the appSettings.

<add key="Set_Css" 

value="App_Themes/Default/Css1.css,App_Themes/Default/Css2.css"/>
  <add key="Set_Javascript" 
value="Javascripts/Js1.js,Javascripts/Js2.js,

http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"/>

After that you have to give the cSS and JS path on the Page1.aspx.

<link   type="text/css" 
            rel="Stylesheet" 
            href="HttpCombiner.ashx?s=Set_Css&t=text/css&v=1" />

    <script     
        type="text/javascript" 
        src="HttpCombiner.ashx?s=Set_Javascript&t=type/javascript&v=2" >
    </script>

Thats all, now you upload the file to server and check the JS,CSS file compressed (check the file length before and after applying this technique) using Fiddler tool or any other tools.
These are some tools to check Page response time.

http://www.aptimize.com/
http://websiteoptimization.com/services/analyze/
http://www.fiddler2.com/

Compressing Asp.Net Pages

Posted by Venkat | Labels: ,

Good Evening to All.
Here I am going to cover how to compress the .aspx pages.
so there are no.of ways to compress the pages in asp.net
1) using IIS.
2) using Code ie: through HTTPCompress.dll i got the source code from codeproject.com.
so i am going to share with you.
There are two type of Compression ie: Deflate , GZIP. here i am going to use GZIP compression to compress the asp.net pages.
First Place the DLL file ie::HTTPCompress.dll to the Bin folder of your project.
Then you have to include some tag on web.config file

<configsections>
  <sectiongroup name="Flanders">
   <section name="HttpCompress" type="Flanders.Library.Modules.HttpCompress.Configuration, HttpCompress">
  </section>
 </sectiongroup>
<flanders>
  <httpcompress compressiontype="GZip">
   <excludedpaths>
    <add path="NoCompression.aspx">
   </add>
   <excludedmimetypes>
    <add mime="image/jpeg">
   </add>
  </excludedmimetypes></excludedpaths>
</httpcompress>
</flanders>
</configsections>
Here i am using GZiP Compression technique. there are two more inner Tags ie: ExcludePaths, ExcludeMimeTypes.
ExcludePaths - It includes the Page Name that don't want to compress.
ExcludeMimeTypes - Include the mime types that dont want to compress the images..
By default images are compressed so no need to compress the image while using compression so our ExcludedMimeTypes tag should be like this.

<excludedmimetypes>
  <add mime="image/jpeg">
  <add mime="image/jpg">
  <add mime="image/png">
  <add mime="image/gif">
</add>
</add>
</add>
</add>
</excludedmimetypes>
Finally we need to add the httpModules

<httpmodules>
   <add> name="HttpCompressModule" type="Flanders.Library.Modules.HttpCompress.HttpModule,HttpCompress"/>
 </add>
</httpmodules>
Download Source Code

Credit goes to :
Ref: http://www.codeproject.com/KB/aspnet/HttpCompress.aspx

Postback not works when using HTTPCompression

Posted by Venkat | Labels: , ,

Some members asked this question on forums. Here is the Solution to overcome this issue.

When i am going to compress the .aspx pages using HTTPCompression, the postback will not works. Because it also compressing the Scripresource.axd, webresource.axd file.

To make the postback works in your project you do not compress the above two files, by adding these code.

Solution:

........,

<httpcompress compressiontype="GZip">
 <excludedpaths>
 <add path="scriptresource.axd">
 <add path="webresource.axd">
</add>
</add>
</excludedpaths></httpcompress>


And one more thing don't compress the images like jpg, gif, jpeg etc.. because its already compressed one. if you compress the image it will degrade the performance. you should the exclude the image from compression.

Solution:

..,
<excludedmimetypes>
 <add mime="image/jpeg">
 </add>
 </excludedmimetypes>

Thanks to all.

Convert String to DateTime using ASP.NET

Posted by Venkat | Labels: , ,

I saw the question repeatedly asking on forums ie: how to convert the String to datetime.

even if someone give solution still the problems exists..,

Error: String is not recognized as a valid DateTime.

Here i am giving the solution to overcome the problem.

First make sure whether you entered is a valid date or not because User may enter alphabets, special symbols etc.. so in order to avoid that , I have to validate the textbox. Here i am using the Regex to to validate the date.

This is my Article to vaildate date using Regex

try
{
 string sDate ="06/05/2010";
// this the regex to match the date ie: dd/MM/yyyy
 string _dateExpression = @"^((0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](?:19|20)\d\d)$";
 Regex chkDate = new Regex(_dateExpression);
 if ((chkDate.IsMatch(sDate))
 {
 // so if the date is valid and its matched you can store the date on DB or do some manipulatio.
 }
 else
 {
 // show invalid date
 }
}
 catch (System.FormatException ex)
 {
 // show invalid date
 }
catch(Exception ex1)
{
throw;
}

So if the user enter other than numbers it shows format exception to avoid this exception. once i caught the FormatException i am showing the Message to user ie:- input is not valid date or something.

Another small manipulation on DateTime

DateTime sDate = new DateTime();

sDate = DateTime.Today; //  Here i am getting like this 5/6/2010

// So i want this format 05/06/2010  - for this i am using Format to Convert the Date as i Want.

string gDate = String.Format("{0:dd-MM-yyyy}", sDate);

Thanks to All.

PayOffers.in