Friday, March 12, 2010

What are relation objects in dataset and how & where to use them?

In a DataSet that contains multiple DataTable objects, you can use DataRelation objects to relate one table to another, to navigate through the tables, and to return child or parent rows from a related table. Adding a DataRelation to a DataSet adds, by default, a UniqueConstraint to the parent table and a ForeignKeyConstraint to the child table.

The following code example creates a DataRelation using two DataTable objects in a DataSet. Each DataTable contains a column named CustID, which serves as a link between the two DataTable objects. The example adds a single DataRelation to the Relations collection of the DataSet. The first argument in the example specifies the name of the DataRelation being created. The second argument sets the parent DataColumn and the third argument sets the child DataColumn.
custDataSet.Relations.Add("CustOrders",
custDataSet.Tables["Customers"].Columns["CustID"],
custDataSet.Tables["Orders"].Columns["CustID"]);

OR
private void CreateRelation()
{
DataColumn parentCol;
DataColumn childCol;
parentCol = custDataSet.Tables["Customers"].Columns["CustID"];
childCol = custDataSet.Tables["Orders"].Columns["CustID"];
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders", parentCol, childCol);
custDataSet.Relations.Add(relCustOrder);
}

What is serialization in .NET? What are the ways to control serialization?

Serialization is the process of converting an object into a stream of bytes. Deserialization is the opposite process of creating an object from a stream of bytes. Serialization/Deserialization is mostly used to transport objects (e.g. during remoting), or to persist objects (e.g. to a file or database).Serialization can be defined as the process of storing the state of an object to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, are converted to a stream of bytes, which is then written to a data stream. When the object is subsequently deserialized, an exact clone of the original object is created.
There are two separate mechanisms provided by the .NET class library - XmlSerializer and SoapFormatter/BinaryFormatter. Microsoft uses XmlSerializer for Web Services, and uses SoapFormatter/BinaryFormatter for remoting. Both are available for use in your own code.
* Binary serialization preserves type fidelity, which is useful for preserving the state of an object between different invocations of an application. For example, you can share an object between different applications by serializing it to the clipboard. You can serialize an object to a stream, disk, memory, over the network, and so forth. Remoting uses serialization to pass objects "by value" from one computer or application domain to another.
* XML serialization serializes only public properties and fields and does not preserve type fidelity. This is useful when you want to provide or consume data without restricting the application that uses the data. Because XML is an open standard, it is an attractive choice for sharing data across the Web. SOAP is an open standard, which makes it an attractive choice.

Delete confirmation message in datagrid

Private Sub myGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles myGrid.ItemDataBound

Select Case e.Item.ItemType Case ListItemType.Item, ListItemType.AlternatingItem
Dim deleteButton As LinkButton = CType(e.Item.Cells(3).Controls(0), LinkButton) deleteButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');")
End Select

End Sub

Private Sub myGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles myGrid.DeleteCommand
If e.CommandName = "Delete"
// 'delete from database commands
BindData()
End If
End Sub

Disable back button on browser

Write a javascript function:

function disableBackButton()
{
window.history.forward();
}

Call it on load of body in html.
i.e. onload="disableBackButton()"

String Manipulations

// Concatenation of the strings.
string start = "This is a ";
string end = "concatenated string";
string result= start + end;

// Inserting Strings into Strings
string first= "Please check this code.";
string second= "understand";
Console.WriteLine(first.Insert(24, second));

// Search and Replace
string sample = "The good programming.";
string result = sample.Replace("good", "excellent"));

// Copying Strings
string myString= "The ultimate programming code.";
string result = string.Copy(myString);

// Extracting Text from a String
string sample = "The quick start tutorial is a must.";
string result = sample.Substring(4); //result will be "quick start tutorial is a must."

//Trim Function
string myString= " String Manipulation " ;
string Result= Name.Trim(); //it will remove the starting and ending space of the string.

// Remove specified number of characters from string
string myString= "The String Manipulation";
string Result= MainString.Remove(1,4);

Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?

Instance constructor is executed when a new instance of type is created and the class constructor is executed after the type is loaded and before any one of the type members is accessed. (It will get executed only 1st time, when we call any static methods/fields in the same class.) Class constructors are used for static field initialization. Only one class constructor per type is permitted, and it cannot use the vararg (variable argument) calling convention.
A static constructor is used to initialize a class. It is called automatically to initialize the class before the first instance is created or any static members are referenced.

What are Satellite Assemblies?

Satellite assemblies are often used to deploy language-specific resources for an application. These language-specific assemblies work in side-by-side execution because the application has a separate product ID for each language and installs satellite assemblies in a language-specific subdirectory for each language. When uninstalling, the application removes only the satellite assemblies associated with a given language and .NET Framework version.

What Is The Difference Between ViewState and SessionState

View state is a client side state management whereas Session is used to manage state at server side.
Session variable can be accessible from all pages in web application. It is scoped to current browser session only.
ViewState persist the values of controls of particular page in the client (browser) when post back operation done. When user requests another page previous page data no longer available.
SessionState persist the data of particular user in the server. This data available till user close the browser or session time completes.