Search Code Shode

Loading...

Thursday, July 28, 2011

Split CSV String into Table in SQL Server


We all have some time bogged down into such situations where we require to update table from Comma/Character Separated Values.

By default there is no function in SQL Server that can split comma separated string into Table (Rows). Following T-SQL is custom made function that can split CSV string into table.

Here is the function that will return table when passed CSV:

CREATE FUNCTION dbo.SplitCSV (@CSVString VARCHAR(8000), @Delimiter CHAR(1))
RETURNS @temptable TABLE (items VARCHAR(8000))
AS
BEGIN
      DECLARE @pos INT;
      DECLARE @slice VARCHAR(8000);

      SELECT @pos = 1;
      IF LEN(@CSVString) < 1 OR @CSVString IS NULL RETURN;

      WHILE @pos!= 0
      BEGIN
            SET @pos = CHARINDEX(@Delimiter,@CSVString);
            IF @pos != 0
                  SET @slice = LEFT(@CSVString, @pos - 1);
            ELSE
                  SET @slice = @CSVString;

            IF( LEN(@slice) > 0)
                  INSERT INTO @temptable(Items) VALUES (@slice);

            SET @CSVString = RIGHT(@CSVString, LEN(@CSVString) - @pos);
            IF LEN(@CSVString) = 0 BREAK;
      END
      RETURN
END
Usage:

SELECT * FROM dbo.SplitCSV ('Computer,Keyboard,Mouse,USB', ',');



Hope it helps!

What Do You Love from Google - Insight


Google has launched a new, mysterious service called What Do You Love. It’s a simple search box, similar to the one on Google’s homepage, but it returns results from more than 20 different Google services, including Google Translate, Trends, YouTube, Maps and Groups.
The results are presented in little boxes that can, in some cases, be expanded by clicking on the icon in the lower right corner. If you need even more results, you can always click the button in the upper right corner of the box and go directly to the chosen service.
The concept reminds us of iGoogle, a service that lets you add simple apps to your own personalized portal. What Do You Love, however, instantly creates a mini-portal about any term you choose, which makes it a nice option for users who don’t have the time to tinker with options and simply want to have access to Google’s many services on one page.
The service, located on the address www.wdyl.com and google.com/whatdoyoulove, bears Google’s copyright mark but very little explanation, and it hasn’t so far been announced on any of Google’s official blogs.

Tuesday, July 26, 2011

How to Email a Web Page As HTML in ASP.NET using C# VB.NET

This post explains How to Email a Web Page As HTML From Your Site or in fact any web page you want (though i would not recommend any with lots of scripts etc in it). It is very useful for email forms to customers etc from your site.

Personally I create an plainer version of the form just for email. I remove any menu functions and other things that are there as part of the website and are not useful in the email - but that is a personal preference.


I have already written a couple of posts regarding How to Send Email in ASP.NET. You can check them here:


Saturday, July 23, 2011

Format Source Code for Blogging Tool





So today here I am, coming up with a Source Code Formatting Tool for Blogging. Previously
I had a post regarding How To Format My Source Code for Blogging? which explained
the use of SyntaxHighlighter to format your source code for blogging. But now I feel it kinda slow down the blog to load a post. So I decided to create a tool that will provide me with Formatted HTML Code of my Source Code to post in a blog. There will be a series of such tools e.g.

  • Format HTML Source Code for Blogging
  • Format XML Source Code for Blogging
  • Format ASPX Source Code for Blogging
  • Format Javascript Source Code for Blogging
  • Format SQL Source Code for Blogging
  • Format C# Source Code for Blogging
  • Format VB.NET Source Code for Blogging
  • Format CSS Source Code for Blogging

and much more. Right now, I have this tool, which will format your HTML/XML/ASPX
source sode for blogging. What you need to do is Paste your Source Code in the given
Text Area and your formatted source code will be provided in the other Text Area.
Just copy it and paste it in your blog post or where ever you want.

Friday, July 22, 2011

SQL Database Generate Password Function

Most of you might have come across need for creating a random password for the users. I prefer using passwords that are generated completely random, containing a-zA-Z0-9 and some special characters, making them tough to guess. However, thinking from the user's perspective those passwords are really hard to remember. So, if you want to leverage complexity of the passwords for making them easy to remember then here is what I have used.

The key concept behind my algorithm is, a set of letters (like Basheer) forming a meaningful name is easy to remember than a set of letters (like 6ZuK3s) that makes no sense. I compiled a table with Given Names pulled from sparkleware. To generate a random password, I pick a random given name and pull out only first 6 characters from it. Then I create a random set of special characters and set of random hex digits from NewID, UUID/GUID. The random given-name part, special chars part, hex digits part all together frame a random password. This is makes password not only easy to remember, compared to completely randomized text, but also somewhat tough to predict them as well.

Please note that this technique might generate passwords that can be predicted using dictionary attack, if considerable precautions were not taken. If your application’s password needs to highly secure then, I suggest you follow completely randomized text generation as detailed in here,


ALTER FUNCTION dbo.udf_generate_password(@newid VARCHAR(50), @rand REAL) RETURNS VARCHAR(100)
AS
BEGIN
--****************************************************
--   FUNCTION: DBO.UDF_GENERATE_PASSWOR
--   AUTHOR: SHYAM K. ARJARAPU
--   PARAMETER: @NEWID = NEWID() A UUID/GUID/NEWGUID
--      @RAND = RAND()
--   REASON: STUPID SQL SERVER DOES NOT ALLOW NON-
--      DETERMINISTIC FUNCTIONS RAND(), GETDATE(), NEWID()
--      INSIDE THE FUNCTIONS.
--  DESCRIPTION: THOUGH RANDOM PASSWORDS ARE SECURE, THEY
--     ARE HARD TO REMEMBER. USERS CAN EASILY REMEMBER
--     GROUP OF LETTERS FORMING A WORD. THE TABLE
--     DBO.TB_PASSWORD_DICTIONARY CONTAINS SET OF FIRST NAMES
--     FROM WHICH A RANDOM NAME IS SELECTED AND A SPECIAL
--     CHARACTER WITH RANDOM STRING IS APPENDED TO MAKE THE
--     PASSWORD NOT ONLY SECURE BUT ALSO ITS EASY TO REMEMBER
--  USAGE:
--     SELECT dbo.udf_generate_password(NEWID(), RAND())
--*****************************************************
 DECLARE @randPassword VARCHAR(100)
 DECLARE @randNewID VARCHAR(10)
 DECLARE @randFirstName VARCHAR(6)
 DECLARE @randSpecialChar VARCHAR(2)
 DECLARE @maxID INT
 DECLARE @CONST_FN_LEN TINYINT
 DECLARE @CONST_NID_LEN TINYINT
 DECLARE @CONST_SPL_LEN TINYINT
 DECLARE @CONST_SPL_CHARS VARCHAR(15)

 --DEFINE THE REQUIRED LENGTHS &amp; SPECIAL CHARS
 SET @CONST_FN_LEN = 6
 SET @CONST_NID_LEN = 4
 SET @CONST_SPL_LEN = 2
 SET @CONST_SPL_CHARS = '@#$^*;,._!'

 --FRAME THE RANDOM TEXT PART
 SELECT @randNewID = LEFT(@newid, @CONST_NID_LEN)

 SELECT @maxID = MAX([KEY_ID])
   FROM dbo.tb_password_dictionary -- Contains KEY_ID Identity(1,1), [Value] Varchar. All the given-names goes here

 --FRAME RANDOM FIRST NAME PART
 SELECT @randFirstName = LEFT([VALUE], @CONST_FN_LEN)
   FROM dbo.tb_password_dictionary
  WHERE [KEY_ID] = CONVERT(INT, @rand *@maxID) + 1

 SET @randSpecialChar = ''

 --FRAME THE SPECIAL CHARTS PART
 WHILE( @CONST_SPL_LEN &lt;&gt; 0)
 BEGIN
  SET @randSpecialChar = @randSpecialChar +
   SUBSTRING(@CONST_SPL_CHARS, CONVERT(INT, @rand * LEN(@CONST_SPL_CHARS)) + 1, 1)
  SET @rand = @rand * @rand
  SET @CONST_SPL_LEN = @CONST_SPL_LEN - 1
 END

 --FRAME THE PASSWORD ALTOGETHER
 SET @randPassword = @randFirstName + @randSpecialChar + @randNewID
 RETURN @randPassword
END


Courtesy: Shyam K. Arjarapu

Wednesday, July 20, 2011

Display Hierarchical Data with Menu Control in ASP.NET using C# VB.NET

This post will show you:

  1. How to store Hierarchical Menu Data in a Single Database Table
  2. How to Display Hierarchical Data with ASP.NET Menu Control using C# and VB.NET.


Each step is explained and illustrated so that you can quickly grasp things from this post to build your web application's menu.

Before we start, lets see what we are trying to display:

Monday, July 18, 2011

Hit Counter for ASP.NET using C# VB.NET

This is a very simple hit counter / page counter to see how many people have visited a web page if you want to show it on a web page. This is a simple user control that is meant to be on one page (i.e., the home page).

The requirements for this are a user control and read/write access to an xml file. Below is the code for the user control counter.ascx

Markup Code:


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="counter.ascx.cs" Inherits="counter" %>
<asp:Label ID="lblCounter" runat="server"></asp:Label>

Saturday, July 16, 2011

Disable Copy HTML Using Javascript

Thinking of a way to make it more difficult for people to copy the contents of your blog? Here's a neat little JavaScript that will disable Copy and Paste.


    <script type="text/javaScript">
    function disableselect(e){
    return false
    }
    function reEnable(){
    return true
    }
    document.onselectstart=new Function ("return false")
    if (window.sidebar){
    document.onmousedown=disableselect
    document.onclick=reEnable
    }
    </script>


Insert the code between the <head> and </head> tag.

However, there is no guarantee way to prevent your contents from being stolen.The JavaScript above can be easily bypassed by an experience internet user. E.g. If the browser's JavaScript is disabled, the code will not work.

Convert Word Document to HTML and Display in Browser using ASP.NET

This is more a code snippet rather than an article or tutorial as it will have very less explanation as the code is simply ready to use. The following snippet allows you to Convert a Microsoft Word document to HTML and display the same in browser using C# and VB.NET in ASP.NET.

HTML Markup
The html markup is very simple it simply has a ASP.Net FileUpload Control and a Upload button. On the click of the Upload button I am processing the uploaded word document and displaying the same in the browser.


<form id="form1" runat="server">

<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>

<asp:Button ID="btnUpload" OnClick="Upload" runat="server" Text="Upload"></asp:Button>

</form>


Since this application only targets Microsoft Word documents. You might want to have some validations so that only word documents can be uploaded hence for that you can read the following article:

Filtering Files based on Extensions in ASP.Net FileUpload Control


Friday, July 15, 2011

Send Bulk Emails using C# VB.NET in ASP.NET

I have been asked zillion times that How to Send Bulk Emails using C# or VB.NET in ASP.NET.

Well, there are hundreds of tutorials all over the internet on sending bulk emails in ASP.NET using C# or VB.NET.

However, I decided to post a basic "how to" for beginners so that they can easily understand the functionality of send bulk emails in ASP.NET.

For this example I will be using Northwind Database.

Step 1:

Thursday, July 14, 2011

Validate File Extension for ASP.NET FileUpload Control

In this article I am explaining How to Create a File Extension Filter for the ASP.Net FileUpload Control

On many occasions there’s a requirement to upload only selected types of files and reject the others

In this article I will explain both Client Side and Server Side validation of files using their extensions

Client Side Validation:

<script type="text/javascript">
    var validFilesTypes = ["bmp", "gif", "png", "jpg", "jpeg", "doc", "xls"];
    function ValidateFile() {
        var file = document.getElementById("<%=FileUpload1.ClientID%>");
        var label = document.getElementById("<%=Label1.ClientID%>");
        var path = file.value;
        var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
        var isValidFile = false;
        for (var i = 0; i < validFilesTypes.length; i++) {
            if (ext == validFilesTypes[i]) {
                isValidFile = true;
                break;
            }
        }
        if (!isValidFile) {
            label.style.color = "red";
            label.innerHTML = "Invalid File. Please upload a File with" +
    " extension:\n\n" + validFilesTypes.join(", ");
        }
        return isValidFile;
    }
</script>



Thursday, July 7, 2011

Facebook Video Chat Is Available Now! Here Is How To Activate It

So finally the Facebook has start announcing its something “awesome”, we have already know about this major upcoming from the sources, as we know Facebook has released the new service of video calling for Facebook users, and according to the report and announcement it will be powered by Skype. So to activate this function on your profile, you have to visit this page and hit on the “Get Started” button, that’s it now you can do video chat through your account, you will see a button of video chat near to the online friend.



Skype has also updated their Windows client with the integration support of Facebook, so you can also do video chat and group chat right from your desktop though Skype and also you can update your statuses. You have to visit this page to activate the video calling in Facebook: https://www.facebook.com/videocalling.


Also check:
Facebook Blog: Video Chat

اشتہارات