Thursday 24 May 2012

Value types and Reference types

Value types and Reference types

Here are the key points:
■ The value of a reference type expression (a variable for example) is a reference,
not an object.
■ References are like URLs—they are small pieces of data that let you access the
real information.
■ The value of a value type expression is the actual data.
■ There are times when value types are more efficient than reference types, and
vice versa.
■ Reference type objects are always on the heap, but value type values can be on
either the stack or the heap, depending on context.
■ When a reference type is used as a method parameter, by default the parameter
is passed by value—but the value itself is a reference.
■ Value type values are boxed when reference type behavior is needed; unboxing
is the reverse process.


Post By : Dipen Shah
Blog By : Dipen Shah

Boxing and Unboxing - c#

What is concept of Boxing and Unboxing ?
Boxing and unboxing act like bridges between value type and reference types. When we convert
value type to a reference type it’s termed as boxing. Unboxing is just vice-versa. When an object
box is cast back to its original value type, the value is copied out of the box and into the
appropriate storage location.

Example: of boxing and unboxing where integer data type are converted in to object
and then vice versa.
int i = 1;
object obj = i; // boxing
int j = (int) obj; // unboxing


Post By : Urvish Patel
Blog By : Dipen Shah

Tuesday 22 May 2012

What is shadowing?

When two elements in a program have same name, one of them can hide and shadow the other
one. So in such cases the element, which shadowed the main element, is referenced.
Below is a sample code, there are two classes “Parent” and “ShadowedParent”. In
“Parent”, there is a variable “x” which is a integer. “ShadowedParent” overrides
“Parent” and shadows the “x” variable to a string.

Example:

Public Class Parent
Public x As Integer
End Class
Public Class ShadowedParent
Inherits Parent
Public Shadows x As String
End Class

Difference between Shadowing and Overriding:
• Overriding redefines only the implementation while shadowing redefines the whole
element.
• In overriding derived classes can refer the parent class element by using “ME” keyword,
but in shadowing you can access it by “MYBASE”.

Post By : Urvish Patel
Blog By : Dipen Shah
Learn New...

Retrieving the content of a GridView cell.

 You often need to retrieve the gridview cell content and you write the long code in your code behind.

Below mentioned script are used to retrieve the cell content of gridview on click.

<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#<%=GridView.ClientID%> tr").filter(":not(:has(table, th))").click(function(e)
{
var $cell = $(e.target).closest("td"); $("#<%=GridView1.ClientID%> td"). removeClass("highlight"); $cell.addClass("highlight"); $("#message").text('You have selected: ' + $cell. text());
});
});
</script>

Post By : Dipen Shah
Blog By : Dipen Shah
Stay Connected...

Monday 21 May 2012

Delete Duplicate Data - SQL

Delete Duplicate Data without Using primary key and Identity key:

Example:
Id
Name
1
Test
1
Test
1
Test

USE Test
Go
WITH Duplicates AS
(
    SELECT Name,Id,
      ROW_NUMBER() OVER (PARTITION BY Id,Name ORDER BY Id) AS 'RowNum'
    FROM dbo.TestDuplicates  
     
)delete FROM Duplicates WHERE RowNum>1
SELECT * FROM dbo.TestDuplicates

After using this:

Id            Name
1              Test

Post By : Urvish Patel
Blog By : Dipen Shah
Stay Tuned...We rocked...

Validating CheckboxList using Jquery

Below script is useful when you want to validate the checkboxlist control.
You just need to add below function on page.

<script language="javascript" type="text/javascript">
function CheckBoxList1_Validation(sender, args)
{
args.IsValid = false;
var cnt = $("#CheckBoxList1 input:checked").length;
args.IsValid = (cnt >= 3);
}
</script>


Post By : Dipen Shah
Blog By : Dipen Shah
Stay Tuned...

Sunday 20 May 2012

Self Join

A self-join is simply a normal SQL join that joins one table to itself. This is accomplished by using table name aliases to give each instance of the table a separate name. Joining a table to itself can be useful when you want to compare values in a column to other values in the same column.
 A join in which records from a table are combined with other records from the same table when there are matching values in the joined fields. A self-join can be an inner join or an outer join. A table is joined to itself based upon a field or combination of fields that have duplicate data in different records. The data-type of the inter-related columns must be of the same type or needs to cast them in same type.

Examples:

USE ProductManagement
Go
SELECT DISTINCT pv1.ProductID, pv1.CategoryID
FROM dbo.Products pv1
INNER JOIN dbo.Products pv2
ON pv1.ProductID = pv2.ProductID
AND pv1.CategoryID = pv2.CategoryID
ORDER BY pv1.ProductID


Post By : Urvish Patel
Blog By : Dipen Shah
Stay tuned for more... 

Saturday 19 May 2012

Get File Name Without Extention

Below code in asp.net get the filename without extention.

DirectoryInfo directoryInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.SendTo)); directoryInfo.GetFiles().Select(file => Path.GetFileNameWithoutExtension(file.Name)).ToArray();

Posted By : Dipen Shah 
Stay Tuned...

Differance Between CLR, CTS, CLS

What is a CLR? Full form of CLR is Common Language Runtime and it forms the heart of the .NET framework.All Languages have runtime and its the responsibility of the runtime to take care of the code execution of the program. Java has Java Virtual Machine, Similarly .NET has CLR.The responsibilities of CLR are Garbage Collection, Code Access Security, Code Verification, IL( Intermediate language ).
What is a CTS?
In order that two language communicate smoothly CLR has CTS (Common Type System). In order that two different languages can communicate Microsoft introduced Common Type System. So “Integer” datatype in VB6 and “int” datatype in C++ will convert it to System.int32 which is datatype of CTS.
What is a CLS(Common Language Specification)? This is a subset of the CTS which all .NET languages are expected to support.Microsoft has defined CLS which are nothing but guidelines that language to follow so that it can communicate with other .NET languages in a seamless manner.
Post By : Dipen Shah Stay Tuned

Always match datatypes in code with the columns in the database

It's important to make sure that your datatypes match across all layers in your application. For example, if a column's datatype is VarChar(50), you should have the code in queries and stored procedures use local variables of the same datatype.

Similarly, the ADO.NET code in the data layer should specify the same datatype and length.


Posted By : Dipen Shah
Stay Tuned
Difference between Web.Config and Machine.Config File

Machine.Config:
i) This is automatically installed when you install Visual Studio. Net.
ii) This is also called machine level configuration file.
iii)Only one machine.config file exists on a server.
iv) This file is at the highest level in the configuration hierarchy.

Web.Config:
i) This is automatically created when you create an ASP.Net web application project.
ii) This is also called application level configuration file.
iii)This file inherits setting from the machine.config

Posted By : Dipen Shah
Stay Tuned

ExecuteReader, ExecuteNonQuery and ExecuteScalar

What is difference between ExecuteReader, ExecuteNonQuery and ExecuteScalar.

ExecuteReader : Use for accessing data. It provides a forward-only, read-only, connected recordset.
ExecuteNonQuery : Use for data manipulation, such as Insert, Update, Delete.
ExecuteScalar : Use for retriving 1 row 1 col. value., i.e. Single value. eg: for retriving aggregate function. It is faster than other ways of retriving a single value from DB.

Posted By : Dipen Shah
Stay Connected

Disallowing cut/copy/paste operations on a TextBox

Below is the simple jquery to not allow user to cut/copy/paste operations on a TextBox

<script type="text/javascript">
$(document).ready(function() {
$('#<%=txtNewPwd.ClientID%>').bind('cut copy paste',
function(e) {
e.preventDefault();
alert("Cut / Copy / Paste disabled in this textbox");
});
$('#<%=txtConfirmNewPwd.ClientID%>').bind('cut copy
paste', function(e) {
e.preventDefault();
alert("Cut / Copy / Paste disabled in this textbox
too!");
});
});
</script>

Post By : Dipen shah
Stay Tuned