Pages

Thursday, 9 July 2015

Splitstring in SQL

Introduction:  This function will split the string given by the character given in SQL.

Code: 

create FUNCTION [dbo].[string_split]
(   
      @string NVARCHAR(MAX),
      @split_character CHAR(1)
)
RETURNS @result TABLE (
      Item NVARCHAR(1000)
)
AS
BEGIN
      DECLARE @StartIndex INT, @EndIndex INT

      SET @StartIndex = 1
      IF SUBSTRING(@string, LEN(@string) - 1, LEN(@string)) <> @split_character
      BEGIN
            SET @string = @string + @split_character
      END

      WHILE CHARINDEX(@split_character, @string) > 0
      BEGIN
            SET @EndIndex = CHARINDEX(@split_character, @string)
          
            INSERT INTO @result(Item)
            SELECT rtrim(ltrim(SUBSTRING(@string, @StartIndex, @EndIndex - 1)))
          
            SET @string = SUBSTRING(@string, @EndIndex + 1, LEN(@string))
      END

      RETURN
END

To excute function:


select item from [string_split] ('Ram, Shyam', ',')




Saturday, 20 June 2015

Code to set ShortCut Keys in winforms

Introduction:
The shortcut keys are used to make your application user friendly and easy to use. But as you know there are some default short keys prepared by Microsoft Windows and some times you need to override these shortkeys.

Code:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.B))  //Key combination
            {
                 // your code here
                return true;    // indicate that you handled this keystroke
            }

            if (keyData == Keys.Control) //Single Key
            {
                txtamount.Focus();
                return true;
            }

            // Call the base class
            return base.ProcessCmdKey(ref msg, keyData);
        }


Code to set default startup page in web.config

Introduction:

When someone opens your website by entering your domain name in the web browser then by default it will show the startup page. If you haven't setup an startup page than it will look for Default.aspx or index.html page in the source files. You can setup your startup page in web.config file.

Code:

  <system.webServer>
    <directoryBrowse enabled="false"/>
    <defaultDocument>
      <files>
        <clear/>
        <add value="Default.aspx"/>
      </files>
    </defaultDocument>
  </system.webServer>

URL mapping in web.config

Introduction :
URL mapping is very useful tool to make your URL look good and meaningful to site user. It will overcome the querystring issue. The user sometimes got confused with the parameters passed in the url in the form of querystring.

Code :
For url mapping you have to pace this code in the web.config file in <system.web></system.web> tag

    <urlMappings enabled="true">
      <add url="~/ComputerCourse" mappedUrl="~/courses.aspx?cid=2"/>
      <add url="~/ComputerHardware" mappedUrl="~/courses.aspx?cid=3"/>
      <add url="~/MobileRepairing" mappedUrl="~/courses.aspx?cid=4"/>
      <add url="~/CuttingTailoring" mappedUrl="~/courses.aspx?cid=5"/>
      <add url="~/Beautician" mappedUrl="~/courses.aspx?cid=6"/>
    </urlMappings>