Posts

Count Number Of Spaces in a String

Hello readers. Below is a simple query to count number of spaces from a string. DECLARE @string VARCHAR ( 200 ) = 'hello how are you' SELECT ( LEN ( @string ) - LEN ( REPLACE ( @string , ' ' , '' ))) Occurence

Get Current IE Version

Hello readers.. IE(Internet Explorer) is the most hectic browser when implementing javascript. Some times we need to write browser specific javascript code, so we need to detect its Version first because different versions of IE run javascript differently. So below is the small piece of code to find current version of IE. The code is for IE only not for others browsers. < html > < head >< title > IE Version Test </ title ></ head > < body > < script type ="text/javascript" language ="javascript">        var IEVer = navigator.appVersion.split( ';' )[1].split( ' ' )[2];        document.write(IEVer); </ script > </ body > </ html >

Split String in SQL SERVER Using XML

Split delimited string using XML. Hello readers.... Below is a simple example to split string by using XML. Create a function that does the task. This function accepts two parameters. First is the string with delimiters e.g.(a,b,c,d – here , are the delimiters)  and second parameter is separator or delimiter (in example , is delimiter). You can use any character for delimiter instead of a comma. I am using coma as a delimiter in the current example. First we convert the string in to XML by using CAST and then by querying over the generated XML we generate the columns that hold the split output of the string. Below is the code. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION fnSplit (       @string VARCHAR ( MAX ),       @seprator VARCHAR ( 1 )     ) RETURNS @chunks TABLE ( Chunks VARCHAR ( 200 )) AS BEGIN       SET @st...

Calculate Running Total

Hello readers..! Some time you need to calculate running total of the item. Many people do extra calculation and long methods to accomplish that. Below is the simple logic to calculate running total.  First create a table that holds item with there price. I m using temporary table.  CREATE TABLE #amount (       RecordId INT IDENTITY ( 1 , 1 ),       ItemName VARCHAR ( 20 ),       Cost INT ) Insert some data into the table.  INSERT INTO #amount VALUES ( 'Pen' , 10 ),( 'Pencil' , 5 ), ( 'Rubber' , 2 ), ( 'Scale' , 2 ), ( 'Paper' , 10 ) Now creating another temporary table (#TotalAmount) from the above table and with extra field named RunningTotal that holds the Running Total of the items. SELECT *, 0 AS RuningTotal INTO #TotalAmount FROM #amount Declaring a variable that calculates the total of item. DECLARE @amountSum INT = 0...

Remove Extra Commas From Beginning or Ending of a String

A simple and very short code to replace extra commas from the beginning or ending of a string. No need to explain the code. DECLARE @str VARCHAR ( 50 ) = ',a,n,i,s,h,' SELECT @str SET @str = REPLACE ( @str , ',' , ' ' ) SET @str = LTRIM ( RTRIM ( @str )) SELECT   REPLACE ( @str , ' ' , ',' )

Open Custom Help File on F1 press

Image
Hello readers, This is new topic for which i have written a code. When we press F1 key then its opens the help for that document and same when we a running any web application or internet and if we press F1 key then it opens the browser help. So here is the scenario, I want to open my own created help file on pressing F1 key. The code is not much complicated. Just we have to trap the F1 key code and cancel its default function and perform the action to open my own created help file that is a word document file. We have to use JavaScript to trap F1 event. After that we retrieve the page URL and its name and open a new popup window by passing file name in query string. To make it more simple to identify which file has to be opened for the current page, I named the help files as the page name. for example if page name is abc.aspx then its help file name is abc.doc. ASPX Page Code   :   <% @ Page Language ="C#" AutoEventWireup ="true" CodeFile ="Default...

Check Content Type of a file before uploading it to server

Hello readers. Below is the small but an important code to check file contents before uploading to server. There may be a chance that the user want to play with the file upload code. Suppose user want to upload a file, but instead of uploading an image file, he changes the extension of a text file from .txt to .jpg and click on upload,,,,, File uploaded. Because code is checking for extension of uploading file not for the content so he can use the trick to upload an invalid file. To prevent this trick read the code. Code is self explanatory and simple. Just by checking the content type of file. Remember that first check the length of file. If it is greater than than 0 then proceed next, because if you rename a text file .txt to .jpg without writing and text in it, its content type becomes the image content type an user is able to upload a invalid image file of size 0 bytes. After this you can use the rest of code to check maximum file size. ASPX Page Cdoe <% @ Page Lan...