Saturday, June 25, 2011

.Net Serializer - you dictate terms !

You may want to look at how you can use the built-in futures of .NET to serialize and deserilize an object into XML, rather than creating ToXML() method on every class that essentially just Data Transfer Object.
I have used these techniques successfully on a couple of projects but don’t have the implementation details handy right now. I will try to update my answer with my own examples sometime later.
Here's a couple of examples that Google returned:
XML Serialization in .NET by Venkat Subramaniam http://www.agiledeveloper.com/articles/XMLSerialization.pdf
How to Serialize and DeSerialize an object into XML http://www.dotnetfunda.com/articles/article98.aspx
Customize your .NET object XML serialization with .NET XML attributes http://blogs.microsoft.co.il/blogs/rotemb/archive/2008/07/27/customize-your-net-object-xml-serialization-with-net-xml-attributes.aspx

SQL : Primary Key and Unique Key

You can use UNIQUE constraints to make sure that no duplicate values are entered in specific columns that do not participate in a primary key. Although both a UNIQUE constraint and a PRIMARY KEY constraint enforce uniqueness, use a UNIQUE constraint instead of a PRIMARY KEY constraint when you want to enforce the uniqueness of a column, or combination of columns, that is not the primary key.
Multiple UNIQUE constraints can be defined on a table, whereas only one PRIMARY KEY constraint can be defined on a table.
Also, unlike PRIMARY KEY constraints, UNIQUE constraints allow for the value NULL. However, as with any value participating in a UNIQUE constraint, only one null value is allowed per column.
A UNIQUE constraint can be referenced by a FOREIGN KEY constraint.
When you declare a UNIQUE constraint, SQL Server creates a UNIQUE index to speed up the process of searching for duplicates. In this case the index defaults to NONCLUSTERED index, because you can have only one CLUSTERED index per table.


Easy way to understand

  • Primary Key column represents the table in terms of uniqueness.
  • Unique key constraint on column is due to a business need or scenario

SharePoint : List Items count and view scopes

We all SharePoint Developers use SP object model . Don't we? What's the difference between. list.Items.Count and list.ItemCount ( list being a SPList object)


Before we answer this Question, Lets see what are different view  scopes present in a list.

View Scope
SPViewScope is a SharePoint enumeration, which has values Default, FilesOnly, Recursive, RecursiveAll.


->Folder 1
         Item 1
         Item 2
->Item 3

Consider a SHAREPOINT list which has items in the above hierarchy.i.e. 'Folder 1' and 'Item 3' directly at the top level and 'Item 1 ,Item 2' inside the 'Folder 1 '.

Now lets set different scopes and check out what items are seen in the view.
Note : The scope can be set to  SPview.Scope or to an SPQuery.Scope

Default
  • Folder1
  • Item3
 FilesOnly
  • Item 3
Recursive
  • Item 1
  • Item 2
  • Item 3
RecursiveAll
  • Folder 1
  • Item 1
  • Item 2
  • Item 3
Now to get back to our previous question. In this case
  • list.Items.Count : 3. Does not include folder, but includes items inside folders
  • list.ItemCount   : 4. Includes folders as well as items inside folders.













Wednesday, June 15, 2011

Http and Https

Stands for HyperText Transfer Protocol, the underlying protocol used by the World Wide Web. HTTP defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands. For example, when you enter a URL in your browser, this actually sends an HTTP command to the Web server directing it to fetch and transmit the requested Web page

The Secure Socket Layer protocol was created by Netscape to ensure secure transactions between web servers and browsers. The protocol uses a third party, a Certificate Authority (CA), to identify one end or both end of the transactions. This is in short how it works.

As we know https is more secured, why do websites use http anyway?
  • One reason is that https cost more. 
  • Another reason is it slows down the website since it encrypts and decrypts every communication a web user sends or receives.


You can place all websites in three categories


1. Least Security – These websites use http throughout. Most internet forums will probably fall into this category. Because these are open discussion forums, secured access is generally not required
2. Medium Security – These websites use https, when you sign in (when you enter your id and password) and use http once you are logged in. Google and Yahoo are example of such sites. MSN (or Hotmail) provides you with an option to use http or https protocol. You can choose ‘Use enhanced security’ option for https or ‘Use standard security’ option for http.
3. Highest security – These websites use https throughout. Most financial institutions fall into this category. Try logging to your bank or credit card company’s website, you will see https protocol being used throughout.


Tip – It is good idea to keep different password for least secured website. So, just in case it is stolen, your secured website will still be safe.

Thursday, June 9, 2011

SQL : Joins and Corelated Subquery

Co-related Subquery

A sub query is a query that is nested inside a SELECT, INSERT, UPDATE, or DELETE statement, or inside another sub query. A sub query can be used anywhere an expression is allowed


/* SELECT statement built using a sub query. */
SELECT Name
FROM AdventureWorks2008R2.Production.Product
WHERE ListPrice =
(SELECT ListPrice
FROM AdventureWorks2008R2.Production.Product
WHERE Name = 'Chainring Bolts' );



/* SELECT statement built using a join that returns
the same result set. */
SELECT Prd1. Name
FROM AdventureWorks2008R2.Production.Product AS Prd1
JOIN AdventureWorks2008R2.Production.Product AS Prd2
ON (Prd1.ListPrice = Prd2.ListPrice)
WHERE Prd2. Name = 'Chainring Bolts';


In some cases they can rank among the poorest performing SQL statements imaginable because the inner result set must be constructed for every single row that is a candidate for inclusion in the outer result set. If both the inner and outer result sets are large the amount of processing required can be huge
If a table appears only in a sub query and not in the outer query, then columns from that table cannot be included in the output.


Conclusion
There are times when a subquery is advisable, but in most cases a JOIN is better. Here is why:

1. Subqueries have to execute first, then the outer query is executed. Basically executing one subquery within your query means you are executing TWO queries instead of one. Joins would eliminate the need for this.

2. A join can take better advantage of the index. If your table is indexed on the joined column, a join is much faster.

3. For ease of reading, joins are much better.

Monday, June 6, 2011

SQL : char, nchar, nchar, nvarchar

  • nchar and nvarchar can store Unicode characters.
  • char and varchar cannot store Unicode characters.
  • char and nchar are fixed-length which will reserve storage space for number of characters you specify even if you don't use up all that space.
  • varchar and nvarchar are variable-length which will only use up spaces for the characters you store. It will not reserve storage like char or nchar.
nchar and nvarchar will take up twice as much storage space, so it may be wise to use them only if you need Unicode support.

You can use char when the data entries in a column are expected to be the same size.
You can use varchar when the data entries in a column are expected to vary considerably in size.

Easy way to Remember
n - Can store Unicode characters
var - variable length

DECLARE @myVariable AS char(40)
SET @myVariable = 'Prasad'
SELECT DATALENGTH(@myVariable)

Answer is 40

=============================================

DECLARE @myVariable AS varchar(40)
SET @myVariable = 'Prasad'
SELECT DATALENGTH(@myVariable)

Answer is 6



To know more about UNICODE go thru Wikipedia or this article
To
know more about other Data Types read this blog
A ver well written FAQ's on SQL .Must Read