Total Pageviews

Monday 5 December 2011

How to Java Application in Eclispe


  •  Download the JDK jdk-6u20-windows-i586.exe and Setup on your machine.
  • Create a Environment Variable name JAVA_HOME value C:\Program Files (x86)\Java\jdk1.6.0_20 

  • Open  \eclipse\eclipse.exe .
  • Go to File->Import then import your sources and run.



Thursday 1 December 2011

How to prevent downloading when user's allocated size or period exceeds

It is handled using httphandler in ASP.NET. The handler code is bellow ..



public class DownloadHandler : IHttpHandler
{
       public DownloadHandler()
       {
              //
              // TODO: Add constructor logic here
              //
       }

    public bool IsReusable
    {
        get { return true; }
    }

    /// <summary>
    /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
    /// </summary>
    /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
    public void ProcessRequest(HttpContext context)
    {
 
        long currentDownloadedAmount = 0;
        try
        {


            // File name
            string fileName = context.Request.Url.Segments[context.Request.Url.Segments.Length - 1]; ;
            fileName = context.Server.UrlDecode(fileName);
            string filePath = @"d:\ATVMovies\" + fileName;

            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            // Total bytes to read
            long dataToRead = fileStream.Length;
            context.Server.ScriptTimeout = 600;
            context.Response.Buffer = true;
            context.Response.Clear();

            context.Response.ContentType = "application/octet-stream";
            context.Response.AddHeader("Content-Disposition",
                                       "attachment; filename=\"" + filePath + "\";");

            context.Response.AddHeader("Content-Length", dataToRead.ToString());

            int ChunkSize = 1024; //256 MB =256*1024



            // Buffer to read 10K bytes in chunk
            byte[] buffer = new Byte[ChunkSize];
            int offset = 0;
            int length;
            long UserRestAmount = MovieHelper.getMovieBalance((Guid)Membership.GetUser().ProviderUserKey);

            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (context.Response.IsClientConnected)
                {

                    // UserRestAmount = MovieHelper.getMovieBalance((Guid)Membership.GetUser().ProviderUserKey);
                    if (UserRestAmount <= 0)
                    {
                        fileStream.Close();
                        dataToRead = -1; //disconnected
                        break;
                    }
                    // Read the data in buffer
                    length = fileStream.Read(buffer, offset, ChunkSize);


                    //offset += ChunkSize;

                    // Write the data to the current output stream.
                    context.Response.OutputStream.Write(buffer, 0, (int)length);

                    // Flush the data to the HTML output.
                    context.Response.Flush();

                    buffer = new Byte[ChunkSize];
                    dataToRead = dataToRead - length;

                    currentDownloadedAmount += length;
                    UserRestAmount -= length;

                }
                else
                {
                    //prevent infinite loop if user disconnects

                    dataToRead = -1;
                }
            }
            if (dataToRead == 0 || dataToRead == -1)  // complete or disconnected
            {
                MovieHelper.UpdateDownloadSize((Guid)Membership.GetUser().ProviderUserKey, currentDownloadedAmount);
                currentDownloadedAmount = 0;



            }

            fileStream.Close();
        }
        catch (Exception ex)
        {
            MovieHelper.UpdateDownloadSize((Guid)Membership.GetUser().ProviderUserKey, currentDownloadedAmount);
            currentDownloadedAmount = 0;
            context.Response.Write(ex.Message);
        }
     

     
    }


   

  
}


Tuesday 29 November 2011

Step by step of ASP.NET membership


Step by step of ASP.NET membership
1.       Run aspnet_regsql.exe which located in C:\Windows\Microsoft.NET\Framework64\v4.0.30319.  This wizard gets connection info and creates some database tables, views, SPs with prefix ‘aspnet_’.
2.       Set in your web.config the following lines
   <connectionStrings>
    <add name="USRDBConnectionString" connectionString="Data Source=172.168.10.40;Initial Catalog=TvLibrary;User ID=sa;Password=sa1234;Max Pool Size=75;Min Pool Size=5;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

<membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="USRDBConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
      </providers>
 </membership>

<profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="USRDBConnectionString" applicationName="/"/>
      </providers>
</profile>

<roleManager>
      <providers>
        <clear />
        <add connectionStringName="USRDBConnectionString" applicationName="/"
          name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
        <add applicationName="/" name="AspNetWindowsTokenRoleProvider"
          type="System.Web.Security.WindowsTokenRoleProvider" />
      </providers>
</roleManager>

3.       Then go menu Website->ASP.NET configuration


How to NOKIA QT



  • 1.       Download Nokia QT SDK for windows  from

  • 2.       Setup it  following the steps described at

  • 3.       Create and run a project following the steps described at
        or

How to eMail using c#


using System;
using System.Net.Mail;

namespace Tools
{
    public class eMail
    {
        public class SMTP
        {
            private string _userName = "";
            private string _password = "";
            private string _smtpserver = "";

            private string _from = "";
            private string _subject = "";
            private string _body = "";

            MailMessage _smtp_mail = new MailMessage();

            public string SMTPServer
            {
                set { _smtpserver = value.Trim(); }
            }

            public string From
            {
                set { _from = value.Trim(); }
            }

            public string Body
            {
                set { _body = value.Trim(); }
            }

            public string Subject
            {
                set { _subject = value.Trim(); }
            }

            public SMTP()
            {
                this._userName = "";
                this._password = "";
                this._smtpserver = "localhost";
            }

            public SMTP(string user, string pass, string smtp_server)
            {
                this._userName = user;
                this._password = pass;
                this._smtpserver = smtp_server;
                MailMessage _mail = new MailMessage();
            }

            public void Credentials(string user, string pass)
            {
                this._userName = user;
                this._password = pass;
            }

            public void AddToRecipient(string recipient)
            {
                _smtp_mail.To.Add(new MailAddress(recipient));
            }

            public void AddBccRecipient(string recipient)
            {
                _smtp_mail.Bcc.Add(new MailAddress(recipient));
            }

            public void AddCcRecipient(string recipient)
            {
                _smtp_mail.CC.Add(new MailAddress(recipient));
            }

            public void Send()
            {
                _send();
            }

            public void Send(string From, string To, string Subject, string Body)
            {
                _smtp_mail.To.Add(new MailAddress(To));
                this._from = From;
                this._subject = Subject;
                this._body = Body;

                _send();
            }

            private void _send()
            {
                //set the addresses
                _smtp_mail.From = new MailAddress(this._from);

                //set the content
                _smtp_mail.Subject = this._subject;
                _smtp_mail.Body = this._body;

                //send the message
                SmtpClient smtp = new SmtpClient(this._smtpserver);

                if (!(_userName.Trim() == "" && _password.Trim() == ""))
                {
                    smtp.Credentials = new System.Net.NetworkCredential(_userName, _password);
                }

                try
                {
                    smtp.Send(_smtp_mail);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
    }
}