Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Sunday, August 16, 2015

How to convert first letter of every word in a sentence to upper in C#



I come across with simple but very interesting problem where I have to make first letter of every word in a sentence to capital. For example if I have sentence "top 5 most advance computer" then it should be displayed as "Top 5 Most Advance Computer".

There are lots of ways to do this 

·         Write for loop and do the string operation
·         Call the VB.NET methods etc.

But I found LINQ help me to do this in simple and clean manner. Here is method for this

public static string ConvertFirstCharOfWordToUpper(string str)
{
    return string.Join(" ", str.Split(' ').Select(x => x.First().ToString().ToUpper() + x.ToLower().Substring(1)));
}
 
Please leave your comments or share this code if it’s useful for you.

Friday, June 19, 2015

1 line of code to preview image without uploading to server in ASP.NET

You say, you don’t want to upload image to server in ASP.NET website but still want to see the preview of image.

Here is a trick.

Create aspx page with below controls.

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Preview" OnClick="Button1_Click" /><br />
<img id="imgAny" runat="server" border="0" />

Write below C# code in button event.

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1 != null && FileUpload1.HasFile)
    {
        imgAny.Src = @"data:image/*;base64," + Convert.ToBase64String(FileUpload1.FileBytes);
    }
}

Just fucking 1 line of code ;)

How it work?

Get the bytes of your image and convert into base 64. 
Set image mime type to image with base 64 and append the image string. 
That's it!!!


Please leave your comments or share this code if it’s useful for you.

Friday, June 5, 2015

How to check occurrence of 404 errors on any web page


It’s always great to avoid any 404,500 errors in your web site. There are several tools available for testers to find out the 404 and 500 errors in whole website. But what about developers? Do we know any tools for it? Actually we never bothered about it because we are programmersJ.

But still folks, there are some easy ways in browsers itself where we can find out the 404 and other errors list.

Chrome
Right click on web page -> select 'Inspect Element' -> select 'Console' tab.

Mozilla
Right click on web page -> select 'Inspect Element with Firebug' -> select 'Console' Tab -> ‘Errors’ tab.

Please leave your comments or share these tips if it’s useful for you.

Friday, May 29, 2015

Best SEO practices for ASP.NET Sitecore projects

If you developing any web applications, your client expect to implement SEO techniques. Here I am specifying some of the useful techniques that should be part of any website.

1.    Meta description and keywords

Add Meta tags for description and keywords in Head section.

<meta name="description" content="Put your description here" />
       <meta name="keywords" content="keyword1, keyword2, keyword3" />

2.    Authority Transfer

Do a 301 redirect to all important pages from old website.

3.    Poor URL Structure:
·         The URL’s should be in lower case
·         There should be no extension in URL like .aspx
·         There should be no spaces in the URL’s
·         Dash(-) sign should replace any spaces in the URL’s

4.    Custom URL provision:

There should be a provision to edit the URL for every page in the Sitecore admin.

5.    301 redirect provision:

Provision for 301 redirect on every page to do redirection from old URL to New URL.

6.    Default Title tags:

H1 tags should be used as default title tag for the page

7.    Open graph tags for Social media:

Implement basic og tags for social media like

·         Title
·         Description
·         URL
·         Image
·         Type

Here is example for this
<head>
<title>The Rock (1996)</title>
<meta property="og:title" content="The Rock" />
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
</head>

Refer http://ogp.me/ for more details

8.    Canonical tag element

There should be a provision for adding canonical URL in every page. By default the current page URL will be used.

Here is the syntax:
<link rel="canonical" href="http://www.yourwebsite.com/">

Please leave your comments or share these tips if it’s useful for you.

Tuesday, May 19, 2015

Different ways to generate unique ID in C#

In every project we need to generate some id which should be unique. I was just wondering what are the options available in .NET to achieve this?

One can go for GUID as it’s a 128 bit integer long.

       Console.WriteLine("GUID = " + System.Guid.NewGuid().ToString());
       //Output - GUID = 54f6461a-59f4-4f9f-9a67-49b18ebc6eb6

This seems to be perfect but check GUID is alphanumeric. What if we anybody want only numbers. Holy shit.

We can use random class for it as

Random random = new Random();
Console.WriteLine("Random = " + random.Next(int.MaxValue).ToString());
       //Output - Random = 1344254902

But random class has limitation of max value of integer. It can generate only 10 digit long number. Probability of generating the duplicate number is high with Random class.

Is there any other option?

Interestingly we have datetime available in C#. We can get date as well as time and can generate long number.

Console.WriteLine(System.DateTime.Now.ToString("yyyyMMddHHmmss"));
//Output – 20150519190734

Its 14 digit long number now.

You say you want longer number than this. Don’t forget milliseconds placeholder. Most interesting part of millisecond is that it’s a 7 digit long. You can use millisecond from single f to 7 times f.

Console.WriteLine(System.DateTime.Now.ToString("yyyyMMddHHmmssfffffff"));
//Output - 201505191907346290105

Its 21 digit long number now which should be unique in its own way.
Don’t be greedy. Don’t try to write “f” more than 7 times otherwise you will get “System.FormatException: Input string was not in a correct format.” Error.

Come on buddy, 21 digits long number [Correct word for this is string] is enough for us.


Let me know if you know any other way to generate it.

Please leave your comments or share this code if it’s useful for you.

Saturday, May 16, 2015

How to fix 404 error for fonts in ASP.NET

My tester informed me that we are getting 404 for woff font file. I checked the presence of woff file in fonts folder. I checked the path of font file. Both these checks are positive and everything was seems to be correct. We were not getting the 404 error for any other font file except woff file. Then I googled it and come to know that people are already facing the same error for woff font file.

This is the problem of MIME type. So there are two ways to solve it.

·         IIS
·         Entry in ASP.NET project’s web.config file

Through IIS
          You to make MIME entry in IIS as below for this font

·         Run IIS Server Manager, Open Mime Types and add following
o    File name extension: .woff
o    MIME type: application/x-font-woff

Through web.config file

You can also add the entry of MIME types in the web config as follows

  <system.webServer>
    <staticContent>
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
    </staticContent>   
  </system.webServer>

I prefer the second option of web.config file as this file is a part of our project and we can change it at any time. But it’s very difficult to get the access of IIS on production or client environment.


Please leave your comments or share this tip if it’s useful for you.