08 May 2013

Generate a random password in C#

There are always need of some common functions in many projects. One of them is random password generation. .NET framework is rich set of in-built functions which are mean to solve some general issues. For the random password generation, there is already in-built function is provided in the .NET framework i.e. Membership.GeneratePassword (NameSpace : System.Web.Security). You can get more details about Membership.GeneratePassword  here

However that function is not fit in each situation. So I have generate one new function which creates password with alpha-numeric values.


public string GetRandomPassword()
{
  string randomChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

  char[] randomElements = randomChars.ToCharArray();

  Random r = new Random();

  StringBuilder builder = new StringBuilder();
  for(int i = 0; i=6; i++)
  {
     int randomChar = r.Next(0, randomElements.Length);
     builder.Append(randomElements[randomChar]);
  }
   return builder.ToString();
}

Above function will generate a random password with alphanumeric values. Length of the password will be 6. For the beginner level users,below is the steps of the functions in order to understand it easily.


  1. Declare a string variable with alphanumeric values and convert it to character array.
  2. With use of Random type variable, loop through 6 times(or up to your requirement) to create 6 character long password in StringBuilder.
  3. Return builder string will give the random password.
Hope this small piece code of will help you to create random password with C#



24 April 2013

Get particular string instance from all stored procedures in the SQL database

I would like to share one issue and its solution with you. There were some award categories like "Platinum", "Gold", "Silver" etc used in SQL stored procedures. Now my client was like to update that categories name with something different.

I was totally unaware of the number of stored procedures that used these categories for comparison or any other task. So my task was to find and update particular string with other string in SQL

To decrease my search, I have used information_schema.routines as below.

SELECT routine_name, routine_definition  FROM information_schema.routines where routine_type = 'procedure' and routine_definition like '%Gold%'


Above query will list out all procedure name and its definition where definition contains word like "Gold" from the given database.

From the filtered list, we can easily apply our changes to the stored procedure.

Hope it will help you to narrow down your search in SQL.

20 June 2011

Find the most occurrence of a character in string using C#

Hi All,
Got time after long time for posting something. :)

Recently I came across to a requirement in one of my project to find the specific
character occurrence from the given string.  There are various
methods to achieve this kind of functionality as individual can have their own logic.
I have tried to achieve this with LINQ and C#. Here I am sharing my efforts.

public void FindCharacterOccurrence(string input)
        {           
            if (!string.IsNullOrEmpty(input))
            {


                var outputMin = (from c in input
                                 where c != ' '
                                 group c by c into g
                                 select new
                                 {
                                     c = g.Key,
                                     count = g.Count(),
                                 }).OrderByDescending(c => c.count).Last();                
                


                var outputMax = (from c in input
                                 where c != ' '
                                 group c by c into g
                                 select new
                                 {
                                     c = g.Key,
                                     count = g.Count(),
                                 }).OrderByDescending(c => c.count).First();


                Response.Write(outputMax.count + ":" + outputMax.c);


                var outputAll = (from c in input
                              where c != ' '
                              group c by c into g
                              select new
                              {
                                  c = g.Key,
                                  count = g.Count(),
                              }).OrderByDescending(c => c.count);




                StringBuilder strOutput = new StringBuilder();
                strOutput.Append("
");
                strOutput.Append("
");
                foreach (var item in outputAll)
                {
                    strOutput.Append("
");
                }
                strOutput.Append("
Character
Count
" + item.c + "
" + item.count + "
");


                ltrOutput.Text = strOutput.ToString();//output to Literal control
            }
        }
Features:
  1. It will not count space.
    If you want to count space then please remove WHERE condition from the query.
  2. You can get minimum
    and maximum character count
    also.
Hope you will find it helpful.

Please feel free to provide your comments/suggestions.

26 July 2010

The value for the 'compilerVersion' attribute in the provider options must be 'v4.0' or later if you are compiling for version 4.0 or later of the .

Hello all,
I had recently faced this issue. I have installed Visual Studio 2010 in my Window 7 operating system and was trying to run my application build on framework 3.5. After almost 3-4 hours search I found the following solutions.

Here we go...


Issue :
When trying to convert a Visual Studio 2008(framework 3.5) application to the Visual Studio 2010(framework 3.5 / 4.0), following error will raised by the visual studio IDE.
The value for the 'compilerVersion' attribute in the provider options must be 'v4.0' or later if you are compiling for version 4.0 or later of the .NET Framework. To compile this Web application for version 3.5 or earlier of the .NET Framework, remove the 'targetFramework' attribute from the element of the Web.config file.
Solution :
  • Go to C:\inetpub\wwwroot\Web.config
  • If it is not available, then please run your WebApplication or WebSite, albeit it has error.
  • Then it will display a yellow page error
  • Consider the path of the web.config file it shows in the error page.(see the image below).

  • Change the following stuff in the web.config
  • Remove the targetframework version from line no 20
  • Remove the pages tag from the web.config also
  • Rebuild your application and You have done it :))
Hope it will be helpful to someone.

09 May 2010

Regular Expression for Day Month Year

Hi All,

In every application generally we need some validation for the day,month and year. Like user should only allowed to input valid date value.

Generally I am using calender control for the date. However recently I need to use some text box instead of the calender control. So I have used following validation expression for validating day,month and year.

Validation Expression for Day
^((([1-9]?)|([1-2][0-9])?)|([3][0-1]?))$

Validation Expression for Month
^((([1-9]?)|([0][1-9])?)|([1][0-2]?))$

Validation Expression for Year
[0-9]{4}

You have to place three text box for a date input and place regular expression validator against each. So in the validation expression property of the regular expression validator , use the above mentioned expression to validate proper input of each.

Hope this small tip will be useful. :)

01 May 2010

Short cut for reusable code in toolbox in Visual Studio

Sometime we need some part of coding frequently in a single or multiple application. What we do in this situation ? Just copy the source code and paste it to the destination.

Now, Visual Studio have a great feature instead of copy-paste the source code. For saving the require code at the toolbox, you just need to follow below 3 simple steps.
  1. Select the code snippets
  2. Drag it to the toolbox --> General tab area
  3. Drop it!!

It is too simple process. Even you can rename the code snippet with the appropriate name also. for that you have to just right click the code snippet and click on the rename option.


Finally, you can use this code snippet in any area of the visual studio application by just drag and drop of the selected code snippet.

Hope this will help you. :)