Learn how to generate the unique string using GUID method in asp.net using c#

in this asp.net tutorial you will learn how to generate the unique string using GUID feature in asp.net. GUID stands for Global unique identifier. GUID is a 128 bit integer that can be used to uniquely identify any thing in database. It can help you like a primary key in your database table. There is no chance of duplication of GUID in your database table. So if you want to use GUID as a primary key then go ahead and use it as a primary key without any hesitation.
I have seen many programmers who use the combination of current date time, random number and some string to make the unique string and then store it in database table to uniquely identify the records. Using GUID you will not go with such hassle.

How to create a GUID in asp.net using c#

You can find the GUID method in the system namespace. The GUID method System.Guid.NewGuid() initializes a new instance of GUID class.


There are also some overloads available for those programmers who want to format the GUID in a particular fashion.


Let's have a look over code below to learn the use of GUID method.

Generating the unique string using GUID method in asp.net using c#

string unique_string =Guid.NewGuid().ToString();

Use these overloads method to format the GUID string.

Guid.NewGuid().ToString("N");
Guid.NewGuid().ToString("D");
Guid.NewGuid().ToString("B");
Guid.NewGuid().ToString("P");

There is also a data type uniqueidentifier in sql server to store the GUID value. If you don't want to use this GUID value through c# then you can use NEWID() fucntion of sql server.


GUID is Everywhere


In windows environment GUID is used everywhere. Open the registry by executing the regedit command in run on your windows system, you can easily see that GUIDs are used extensively to uniquely identify every application, they serve like application IDS under the HKEY_CLASSES_ROOT section (AppID key).


Typical format of GUID


936DA01F-9ABD-4d9d-80C7-02AF85C822A8


So this is the way to generate the unique string using GUID method in asp.net using c#.
Read more...

Limit the text length in textbox using javascript

Sometimes you may get requirement to limit the text length in textbox using javascript. It is better approach to limit the text length in textbox because during development you must think that people can harm your whole database by entering the malicious text of thousands of characters if you don’t have applied any text limit in your input textbox or asp:textbox. If malicious text of thousands of characters is provided through textbox then two things can happen


1) Page will show an error after getting respond from database because the column of the table that will store the data might be have width of 100 characters.

2) Incase user enters the script code of thousands of characters then your website may be down or can have other related problem


So bottom line is ALWAYS LIMIT THE TEXT LENGTH IN TEXTBOX. In this tutorial I am covering this topic using javascript the client side language but later on in this blog you will find how to achieve same thing using asp.net the server side because javascript might be disable in user’s/hacker’s system. I write user’s because I believe user can also harm your database unintentionally if you have not taken any precautions.

Now let's have a look over how to apply limit to the text length in textbox either it is Singleline or Multiline.

Limit the text length in textbox using javascript


<html>
<head>
<title>
Limit the text length in textbox using javascript
</title>
<script language="javascript" type="text/javascript">
function checkLength(textboxID)
{
var maxLen; // max number of characters allowed
if(textboxID=="txtFirstName")
{
maxLen=20;
}
else if(textboxID=="txtLastName")
{
maxLen=30;
}
else
{
maxLen = 100;
}


var getValue;
getValue = document.getElementById(textboxID).value;
if (getValue.length > maxLen)
{
document.getElementById(textboxID).value = document.getElementById(textboxID).value.substring(0, maxLen);
}
}
</script>
</head>
<body>
<form>
<input type="text" id="txtFirstName" name="txtFirstName" onkeydown="checkLength(this.id);" onblur="checkLength(this.id);" /><br/><br/>
<input type="text" id="txtLastName" name="txtLastName" onkeydown="checkLength(this.id);" onblur="checkLength(this.id);" /><br/><br/>
<textarea id="txtDescription" rows="10" cols="10" onkeydown="checkLength(this.id);" onblur="checkLength(this.id);">
</textarea>
</form>
</body>
</html>

I could apply the text limit in textbox without using any single line code of javascript by using maxlength property of textbox like given below


<input type="text" id="txtDescription" name="txtDescription" maxlength="2"/>


The maxlength attribute of textbox works fine when textbox is in singleline mode but what about if textbox is in multiline mode? If textbox is multiline (i;e; <textarea> in html) then you have to use the above mentioned javascript custom function and have to call that function using onkeydown and onblur event of multiline textbox.


For asp:Textbox

<script language="javascript" type="text/javascript">
function checkLength(textboxID)
{
var maxLen; // max number of characters allowed
if(textboxID=="txtFirstName")
{
maxLen=20;
}
else if(textboxID=="txtLastName")
{
maxLen=30;
}
else
{
maxLen = 100;
}


var getValue;
getValue = document.getElementById(textboxID).value;
if (getValue.length > maxLen)
{
document.getElementById(textboxID).value = document.getElementById(textboxID).value.substring(0, maxLen);
}
}
</script>


<asp:TextBox ID="txtFirstName" runat="server" EnableTheming="false" Width="200px" onkeydown="checkLength(this.id);" onblur="checkLength(this.id);"></asp:TextBox><br/><br/>


<asp:TextBox ID="txtLastName" runat="server" EnableTheming="false" Width="200px" onkeydown="checkLength(this.id);" onblur="checkLength(this.id);"></asp:TextBox><br/><br/>


<asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" Width="400px" EnableTheming="false"
Rows="5" cols="8" onkeydown="checkLength(this.id);" onblur="checkLength(this.id);"></asp:TextBox>

I have used onblur event because I know many hackers can use copy/paste to enter malicious text of thousands of characters and onblur() event will be proved as a safeguard on that scenario.


So this is the way to limit the text length in textbox using javascript.
Read more...