Friday, September 11, 2009

Compression Decompression in .NET

DeflateStream and GZipStream classes present in System.IO.Compression namespace provide compression and decompression capabilities. GZipStream compresses the serialized content of application, with great results. These classes operate on file streams.

Compression_Decompression_DotNET


The compression through these classes will likely be less than what can be achieved with a file compression utility. But the good thing is that we have this functionality include in .NET Framework 2.0 and above. As Microsoft said this namespace will be further enhanced and modified in the next versions.

Clean Digital Camera Lens with a Vacuum Cleaner

Sometimes dust enters the inside of the lens of digital cameras and results in black spots visible on the display and on pictures. This trick involves getting rid of them with a vacuum cleaner, when all else has failed and the costs of cleaning the inside of the lens are prohibitive compared to the cost of buying a new camera.

Steps

  1. Roll up plastic wrap, forming a thin sausage and leaving a section unrolled.
  2. Wrap the plastic wrap around the opening so that the sausage is on the opening of the vacuum hose.
  3. Turn on your camera and schedule auto off time, if enabled, on a long time, or disable it so the camera won't shut off (and cover the lens) during the operation.
  4. Connect the vacuum hose and camera so that the camera completely plugs the opening. The plastic wrap will help create an airtight seal.
  5. Turn on the vacuum cleaner at the highest power and wait a few seconds. Make sure that the appliance is perfectly joining the opening.
  6. Turn off the vacuum and pull it off of the camera. Take a few test shots and see if it worked!

Tips

  • To reduce risk of damage to the camera, start with a lower vacuum power. If it doesn't work, try increasing the power until it either works, or the camera doesn't work any more.

Warnings

  • This is a last resort method for cleaning the inside of a lens, not the outside, it may well break your camera.

Things You'll Need

  • Camera
  • Vacuum cleaner with hose without accessories.








Wednesday, September 9, 2009

When do we use the UPDATE_STATISTICS command?

This command is basically used when a large processing of data has occurred. If a large amount of deletions any modification or Bulk Copy into the tables has occurred, it has to update the indexes to take these changes into account. UPDATE_STATISTICS updates the indexes on these tables accordingly.

The following stored procedure can be used to run the “Update_Statistic” command on all the tables in your database:

==============

create procedure Update_Statistics
as

set nocount on

declare @tablename varchar(150)

declare table_name cursor for
select ‘UPDATE STATISTICS ‘ + name from sysobjects where type = ‘u’
order by name

open table_name

fetch next from table_name
into @tablename

WHILE @@FETCH_STATUS = 0

BEGIN
exec (@tablename)
print @tablename
FETCH NEXT FROM table_name INTO @tablename
END
print ”
print ‘All user table statistics have been updated’

CLOSE table_name
DEALLOCATE table_name

Tuesday, September 8, 2009

Spaghetti with Mussels and Clams

Preparation Time:15 Mins, Cooking Time: 15 Mins

Ingredients:
500g mussels
500g clams
1/4 cup (60ml) water
1/4 cup (60ml) dry white wine
500g spaghetti
1/3 cup (80ml) extra virgin olive oil
2 cloves garlic, crushed
1 small red thai chilli, chopped finely
2 medium tomatoes (380g), seeded, chopped coarsely
1/2 cup coarsely chopped fresh flat-leaf parsley

Procedures:
1. scrub mussels; remove beards. Rinse clams

2. Combine the water and wine in large saucepan;bring to a boil.
Add mussels and clams; reduce heat, simmer, covered, about 5 mins or until mussels open (discard any that do not). Strain cooking liquid through five sieve into medium bowl; reserve 1/3 cup, discard remainder. Strain remaining 1/3 cup of the cooking liquid again,into small jug. Cover mussels and clams to keep warm.

3. Cook pasta in large saucepan of boiling water, uncovered, until just tender

4. Meanwhile, heat oil in large frying pan; cook garlic and chili, stirring, until fragrant. Add tomato and reserved cooking liquid; bring to a boil.

5. Place drained pasta, mussels, clams and tomato mixture in large bowl with parsley; toss gently to combine all of the stuff.

How to convert XML file in to a class file using c#?

The quick way is to use tools such as xsd.exe or XsdObjectGen.exe.

These tools allow you to quickly generate C# or VB classes from an xsd file.

Here’s what to do:
1) Open an xml file in Visual Studio
2) Create an xsd schema. (’XML\Create Schema’)
3) If necessary, edit the schema file
4) Open a Visual Studio command window and run one of the tools
5) Edit the generate classes (if necessary)

Example:
Code:

C:\projects\myproject\xsd.exe MyXmlClass.xsd /classes /n:CG.MyNamespace

The above command will generate a MyXmlClass.cs file that you include in your project.

To load up the class, it’s as easy as:
Code:

XmlSerializer serializer = new XmlSerializer( typeof( MyXmlClass ) ); using( XmlReader reader = XmlReader.Create( file ) ) { MyXmlClass myXmlClass = ( MyXmlClass )serializer.Deserialize( reader ); }

Saving data from the class back to an Xml file is equally as easy.

NOTE: Feel free to hand edit the generated files (but understand if you regenerate them, your changes will be lost). I generally use the xsd.exe tool first and then hand edit. Xsd.exe will generate any collections using raw arrays (e.g. myField[]) which I don’t like to use because users of the class need to always check for null in order to loop the items. So I hand edit and replace these arrays with generic List<>’s.

Monday, September 7, 2009

Implement a simple captcha in C#.NET

1. Create a page with name “Captcha.aspx”

2. Place the following html code in body part of the page

   1: IMG height="30" alt="" src=BuildCaptcha.aspx width="80">
   2: asp:TextBox runat="Server" ID="txtCaptcha">
   3: "Server" ID="btnSubmit"
   4:            OnClick="btnSubmit_Click"
   5:            Text="Submit" />
   6:
   7: 3. On “btnSubmit_Click” place the following code
   8:
   9: if (Page.IsValid && (txtCaptcha.Text.ToString() ==
  10:      Session["RandomStr"].ToString()))
  11: {
  12:   Response.Write("Code verification Successful");
  13: }
  14: else
  15: {
  16:   Response.Write( "Please enter info correctly");
  17: }

3. Include the following code in “BuildCaptcha.aspx

   1: Bitmap objBMP = new Bitmap(60, 20);
   2: Graphics objGraphics = Graphics.FromImage(objBMP);
   3: objGraphics.Clear(Color.Wheat);
   4: objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
   5:
   6:
   7: //' Configure font to use for text
   8: Font objFont = new Font("Arial", 8, FontStyle.Italic);
   9: string randomStr = "";
  10: char[] myArray = new char[5];
  11: int x;
  12:
  13: //That is to create the random # and add it to our string
  14: Random autoRand = new Random();
  15: for (x = 0; x <> 
  16: {
  17:   myArray[x] = System.Convert.ToChar(autoRand.Next(65,90));
  18:   randomStr += (myArray[x].ToString());
  19: }
  20:
  21: //This is to add the string to session, to be compared later
  22: Session.Add("RandomStr", randomStr);
  23:
  24: //' Write out the text
  25: objGraphics.DrawString(randomStr, objFont, Brushes.Red, 3, 3);
  26:
  27: //' Set the content type and return the image
  28: Response.ContentType = "image/GIF";
  29: objBMP.Save(Response.OutputStream, ImageFormat.Gif);
  30: objFont.Dispose();
  31: objGraphics.Dispose();
  32: objBMP.Dispose();

4. There you go you can test the page with captcha. Happy Programming!