Thursday, December 6, 2012

Checking a Stored Procedure exists or not in database


When we keep all our stored procedures creation script in a file and want to run that whole script. It always complains about already existing stored procedure having same name.
It is better before running your Stored Proc creation script, your creation script should be smart enough that always checks the presence of same name stored procedure before.
 
Following piece of SQL Script checks the stored procedure presence. If it finds, it delets the old one and create a new one having same name (of course most of the time, the new one would be modified)
When we keep all our stored procedures creation script in a file and want to run that whole script. It always complains about already existing stored procedure having same name.

It is better before running your Stored Proc creation script, your creation script should be smart enough that always checks the presence of same name stored procedure before.

Following piece of SQL Script checks the stored procedure presence. If it finds, it delets the old one and create a new one having same name (of course most of the time, the new one would be modified)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StoredProcedureName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ StoredProcedureName]
GO

Thanks,
Hemant

Tuesday, November 27, 2012

Hangman Game (Guess the Word) in C#

This word guessing game is very similar to popular word guessing game: HANGMAN
(Only difference is that here computer is not drawing "Hangman" picture on every wrong guess by the guessing player... May be in next version, I might include that diagram...)

This game is developed as a Windows Application in C#. Feel free to download the game... and  source code of this game.


 

Screen Shots:




Rules:

This game is generally played by two persons. One player has to guess a word which is given by another player. Here you have to play this game with Computer. In order to win this game, Guessing player has to guess the word correctly. In the guessing, player can not make more then five wrong guesses.
If he suggests more than five wrong letters, he loses the game.
Initially, the word to guess is represented by a row of dashes, giving the number of letters.
If the guessing player suggests a letter which occurs in the word, the other player (in this case Computer) writes it in all its correct positions.
If the suggested letter does not occur in the word, guessing player loses his guessing attempt.
The game is over when:
  • The guessing player completes the word, or guesses the whole word correctly 
  • The guessing player makes more than five wrong guesses and unable to guess the word correctl

Friday, November 23, 2012

Image Color Detector in C#


 In this article I am going to show how we can detect any particular color in an image in C#. A sample code is also attached along with this article for your reference.



 
In this “Color Detector” application, firstly we select an image (jpeg, bmp, png etc.) and then we pick the color which we want to detect on that image. If we found the color, we display a confirmation message. The snapshot for the running application is below:


Now we go to in more detail of this application:

Source of the image:  We can take mage from any source (like Digital Camera, any stored image in PC, Mobile etc.), and keep the image somewhere in our system. Image can be in any format (i.e. jpeg, bmp, png etc.)

Image Selection: For image file selection, we include an “OpenFileDialog” control into our form. On “Choose Image” button click, we show “OpenFileDialog” to choose any image file from the system. After image file selection, we get the file path and then load the image into a picture box. For the file selection and loading into a picture box, refer to following code:
   private void ChooseImageBtn_Click(object sender, EventArgs e)
        {
            try
            {
                //Clearing previously selected image from picture box
                ImagePathLbl.Text = "";
                pictureBox1.Image = null;
 
                //Showing the File Chooser Dialog Box for Image File selection
                DialogResult IsFileChosen = openFileDialog1.ShowDialog();
 
                if (IsFileChosen == System.Windows.Forms.DialogResult.OK)
                {
                    //Get the File name
                    ImagePathLbl.Text = openFileDialog1.FileName;
 
                    //Load the image into a picture box
                    if (openFileDialog1.ValidateNames == true)
                    {
                        pictureBox1.Image = Image.FromFile(ImagePathLbl.Text);
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

Color Selection: For color selection which we want to detect in our selected image, we include an “ColorDialog” control into our form. On “Pick Color” button click, we show “ColorDialog” to choose any color from the dialog box. After color selection, we get the color name and then display the picked color into a panel by setting Panel.BackColor property to picked color. If the picked color is a known color, we also display the color name in text. For the color selection and displaying the selected color, refer to following code:

        private void PickColorBtn_Click(object sender, EventArgs e)
        {
            try
            {
                // Clean previously selected color
                SelectedColorNameLbl.Text = "";
                panelSelectedColor.BackColor = actualColor;
 
                //Showing color choice
                DialogResult IsColorChosen = colorDialog1.ShowDialog();
 
                if (IsColorChosen == System.Windows.Forms.DialogResult.OK)
                {
                    panelSelectedColor.BackColor = colorDialog1.Color;
 
                    //If it is know color, display the color name  
                    if (colorDialog1.Color.IsKnownColor == true)
                    {
                        SelectedColorNameLbl.Text = colorDialog1.Color.ToKnownColor().ToString();
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

Color Detection: For color detection, firstly we convert the loaded image in picture box to a “BitMap” class. Refer to following MSDN link for more detail:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx

Then we iterate each pixel of image and get the pixel color. We store the each pixel color into a temporary variable named “now_color”. Get the 32-bit ARGB value of this “now_color” (a Color Object) with 32-bit ARGB value of our “Picked Color”. If both values are same, we display a “Color Found!” message and come out from the iteration, otherwise we keep on looking other pixel until we cover whole image. If we don’t get success in color finding, we show “Selected Color Not Found" Message. For the color detection, refer to following code:




 private void DetectColorBtn_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean IsColorFound = false;
 
                if (pictureBox1.Image != null)
                {
                    //Converting loaded image into bitmap
                    Bitmap bmp = new Bitmap(pictureBox1.Image);
 
                    //Iterate whole bitmap to findout the picked color
                    for (int i = 0; i < pictureBox1.Image.Height; i++)
                    {
                        for (int j = 0; j < pictureBox1.Image.Width; j++)
                        {
                            //Get the color at each pixel
                            Color now_color = bmp.GetPixel(j, i);
 
                            //Compare Pixel's Color ARGB property with the picked color's ARGB property 
                            if (now_color.ToArgb() == colorDialog1.Color.ToArgb())
                            {
                                IsColorFound = true;
                                MessageBox.Show("Color Found!");
                                break;
                            }
                        }
                        if (IsColorFound == true)
                        {
                            break;
                        }
                    }
 
                    if (IsColorFound == false)
                    {
                        MessageBox.Show("Selected Color Not Found.");
                    }
                }
                else
                {
                    MessageBox.Show("Image is not loaded");
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }