Thursday, October 12, 2006

Generating a dynamic registration image for web application registration page

This focuses on generating an image on the fly in ASP.NET. This feature you may find on any website that requires registration of a user. Example: Yahoo Registration, this site requires you to type in your secret string before you can successfully login.

This focuses on generating an image on the fly in ASP.NET. This feature you may find on any website that requires registration of a user. Example: Yahoo Registration, this site requires you to type in your secret string before you can successfully login.

Step 1: Create a new web application

Step 2: Open Code Behind Form of web application you have created.

Step 3: Add Namespace for drawing Image.

Creating a registration image on the fly

using System.Drawing;
using System.Drawing.Imaging;


Step 4: Write the Code for Generating a Random Registration String (Here: Generating Random string of 10 characters in length).

    // Generate a random password with the specified length. The password will only 
// contain digits and letters (either lowercase or uppercase)
public static string GetRandomPassword(int length)
{
Random rand = new Random();
System.Text.StringBuilder password = new System.Text.StringBuilder(length);
for (int i = 1; i <= length; i++)
{
int charIndex;
// allow only digits and letters
do
{
charIndex = rand.Next(48, 123);
} while (!((charIndex >= 48 && charIndex <= 57) ||
(charIndex >= 65 && charIndex <= 90) || (charIndex >= 97 && charIndex <= 122)));


// add the random char to the password being built
password.Append(Convert.ToChar(charIndex));

}
return password.ToString();
}


Step 5: Write Code for Generating Image. This code will create RegistrationImg.gif in your application folder.

    //Generate Image
private void GenerateImage(string strRegistrationStr)
{
Bitmap objBitmap = new Bitmap(150, 30);
Graphics objGraphics = Graphics.FromImage(objBitmap);


//Fore Color and Background of Image
SolidBrush objForeColor = new SolidBrush(Color.White);
SolidBrush objBackColor = new SolidBrush(Color.Black);

objGraphics.FillRectangle(objBackColor, 0, 0, 150, 30);

//Font settings for Image
Font objFont = new Font("Arial", 15);

//Display from 5point from x-axis and 5point from y-axis
Point objPoint = new Point(5, 5);

//Drawing Registration String
objGraphics.DrawString(strRegistrationStr, objFont, objForeColor, objPoint);

//Saving Registration String as Image
//If you dont want image to save
//objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
//otherwise use this
objBitmap.Save(Server.MapPath("RegistrationImg.gif"), ImageFormat.Gif);


//Release object
if(objBitmap != null)
objBitmap.Dispose();
if (objGraphics != null)
objGraphics.Dispose();
}


Step 6: Finally the code for calling the created functionality.

   protected void btnShowRegistrationImg_Click(object sender, EventArgs e)
{
GenerateImage(GetRandomPassword(10));
ImgRegistrationStr.ImageUrl = "RegistrationImg.gif";
}
Get more information
Can't find what you're looking for? Try Google Search!
Google
 
Web eshwar123.blogspot.com

A simple multithreading .NET application example

Threads are also known as lightweight processes.  However, if we go into depth then we would know that a thread is not actually a process; rather it provides ways for executing different parts of a program.  Now let us discuss what it actually means by multithreading.  Multithreading (as the name suggests multi+threading) is nothing but an efficient execution of multiple threads at a time to enhance the performance of the application.  For example, we are doing a file copy operation with a status bar on UI indicating the completion percentage.  Here we need to keep track of how much file size is copied and at the same time we also need to advance the progress bar accordingly.  This can not be done efficiently in a single thread and you have to use multiple threads.

Multithreading

Whenever an application runs, it runs under a main thread.  However, running a single thread can sometimes lead to unnecessary performance and locking issues.  So if the application can be broken into multiple threads without hampering the flow of the main thread, then using it is always better.

Figure 1

a simple multithreading .net application example

The following care needs to be taken while going for multithreading.

While using the threads we need to be very careful about the flow of each individual thread. Otherwise, there is a heavy probability of deadlock creation when multithreading comes into picture.

All the sub-internal threads need to be ended before the main thread ends.

Benefits of Multithreaded Applications

The image given below shows the difference between the single threaded and multi-threaded applications.  In a single threaded application, the flow gets confined to one thread only.  Therefore, one independent portion of the application may also have to wait for a long time to get executed, resulting in an overall increase of Response and execution time.  In a multithreading environment the independent section of an application gets executed by the separate threads and can continue execution by also doing time-span overlaps.  So at any particular instant there is every possibility of execution of more than one thread, resulting in a significant rise in performance.

Figure 2

a simple multithreading .net application example

In .NET, the threading is handled through the System.Threading namespace.

Creating a variable of the System.Threading.Thread type allows you to create a new thread to start working with.

It is clear to everybody that the concept of threading is to go off and do another task.  The Thread constructor requires the address of the procedure that will do the work for the thread.

The AddressOf is the parameter that the constructor needs to begin using the thread.

Below is an example of a simple threading application.

Imports System.Threading
Public Class Form1
  Inherits System.Windows.Forms.Form
 
  Dim th1 As Thread
  Dim th2 As Thread
 
  Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
    th1 = New Thread(AddressOf proc1)
    th2 = New Thread(AddressOf proc2)
 
    th1.Start()
    th2.Start()
  End Sub
 
  Sub proc1()
    Dim iCount As Integer
    For iCount = 1 To 10
      cmb1.Items.Add(iCount)
    Next
  End Sub
  Sub proc2()
    Dim iCount As Integer
    For iCount = 11 To 20
      cmb2.Items.Add(iCount)
    Next
  End Sub
End Class

Get more information

Can't find what you're looking for? Try Google Search!
Google
 
Web eshwar123.blogspot.com

Tuesday, October 10, 2006

Need for Denormalization of Tables in database

Denormalization is the process of putting one fact in numerous places. This speeds data retrieval at the expense of data modification.Only one valid reason exists for denormalizing a relational design - to enhance performance. However, there are several indicators which will help to identify systems and tables which are potential denormalization candidates. These are:

  • Many critical queries and reports exist which rely upon data from more than one table. Often times these requests need to be processed in an on-line environment.
  • Repeating groups exist which need to be processed in a group instead of individually.
  • Many calculations need to be applied to one or many columns before queries can be successfully answered.
  • Tables need to be accessed in different ways by different users during the same timeframe.
  • Many large primary keys exist which are clumsy to query and consume a large amount of DASD when carried as foreign key columns in related tables.
  • Certain columns are queried a large percentage of the time. Consider 60% or greater to be a cautionary number flagging denormalization as an option.

Be aware that each new RDBMS release usually brings enhanced performance and improved access options that may reduce the need for denormalization. However, most of the popular RDBMS products on occasion will require denormalized data structures. There are many different types of denormalized tables which can resolve the performance problems caused when accessing fully normalized data. The following topics will detail the different types and give advice on when to implement each of the denormalization types.

Get more information

Can't find what you're looking for? Try Google Search!
Google
 
Web eshwar123.blogspot.com

Rules to be checked before denormalizing the tables

Frequently, however, performance needs dictate very quick retrieval capability for data stored in relational databases. To accomplish this, sometimes the decision is made to denormalize the physical implementation. Denormalization is the process of putting one fact in numerous places. This speeds data retrieval at the expense of data modification.

It is not the intention of this article to promote the concept of denormalization. Of course, a normalized set of relational tables is the optimal environment and should be implemented for whenever possible. Yet, in the real world, denormalization is sometimes necessary. Denormalization is not necessarily a bad decision if implemented wisely. You should always consider these issues before denormalizing:

  • can the system achieve acceptable performance without denormalizing?
  • will the performance of the system after denormalizing still be unacceptable?
  • will the system be less reliable due to denormalization?

If the answer to any of these questions is "yes," then you should avoid denormalization because any benefit that is accrued will not exceed the cost. If, after considering these issues, you decide to denormalize be sure to adhere to the general guidelines that follow.

If enough DASD is available at your shop, create two sets of tables: one set fully normalized and another denormalized. Populate the denormalized versions by querying the data in the normalized tables and loading or inserting it into the denormalized tables. Your application can access the denormalized tables in a read-only fashion and achieve performance gains. It is imperative that a controlled and scheduled population function is maintained to keep the data in the denormalized and normalized tables synchronized.

If DASD is not available for two sets of tables, then maintain the denormalized tables programmatically. Be sure to update each denormalized table representing the same entity at the same time, or alternately, to provide a rigorous schedule whereby tables will be synchronized. At any rate, all users should be informed of the implications of inconsistent data if it is deemed impossible to avoid unsynchronized data.

When updating any column that is replicated in many different tables, always update it everywhere that it exists simultaneously, or as close to simultaneously as possible given the physical constraints of your environment. If the denormalized tables are ever out of sync with the normalized tables be sure to inform end-users that batch reports and on-line queries may not contain sound data; if at all possible, this should be avoided.

Finally, be sure to design the application so that it can be easily converted from using denormalized tables to using normalized tables.

Get more information

Can't find what you're looking for? Try Google Search!
Google
 
Web eshwar123.blogspot.com