Thursday, 30 August 2012

Note : This application contains both serialize and deserialize exmples. Goto File menu and Download after click on download link.

In XML Deserialization you convert XML file data back in to custom class objects.
This is a reverse process of XML Serialization.In previous examplewe serialize on object data in to XML now we take this Generated XML file and create class object from that.

This XML Serialization and Deserialization is very useful when communicating between to system where from one system object's data is converted into XML and transmit through network and at receiver end other system receive that xml data and back to converted object using deserialize.This is useful when both end has different platform. I.e one end has .Net application and other end has JAVA or PHP or anyother patform applicaton.

Here is example for this.
In this example we took XML serialized file "ProductDetails.xml" and deserialize this into object with populating data back.
You can see in the output image that "ProductCategory" object fill from the "ProductDetails.xml" XML File.

Wednesday, 29 August 2012

Note : This application contains both serialize and deserialize exmples. Goto File menu and Download after click on download link.

There is a need where you want objects data into XML format.
You can achieve this using XML Serialization.
Using XML Serialization you can convert custom objects data into XML and XML into custom objects.We are using "XmlSerializer" class available in "System.Xml.Serialization" namespace.
There is some prerequisite for custom object class that need to know.

The classes that are going to serialize must have a default zero-argument constructor. XmlSerializer use this constructor at a time of creating the new object during deserialization process.

XmlSerializer serializes only public variables and properties.

All class properties must be readable and writable. Because XmlSerializer uses the property to retrieve information and the property to restore the data after deserialization.

To use XML serialization, you must do mark up data objects with attributes that indicate the desired XML mapping. These attributes are available in the System.Xml.Serialization namespace.

XmlRoot indicate the name of the root element of the XML file. By default, XmlSerializer will use the name of the class. This attribute apply to the class declaration.

XmlElement indicates the element name that use for a property or public variable. By default, XmlSerializer will use the name of the property or public variable.

XmlAttribute indicate that a property or public variable serialized as an attribute.

XmlIgnore indicate that a property or public variable should not be serialized in serialization process.

XmlEnum indicate that the text that should be used at the time of serializing enumerated values.

Here is example for this.
In this example we serialize object into XML.
We are taking two classes "ProductCategory" and "Product". We create object of this class fill some data and generate xml using XMLSerialization.

Tuesday, 28 August 2012

There is a need in application to upload a file and store into SQL Server Database table.
You can do this very simple way.
File is stored in sql server as binary data. You need to set "varbinary" data type of the column in which you want to store file. You need to take "varbinary(MAX)" data type.

Here is example for this.
In this example we take one "product_master" table. This tables contains columns like "product_id" , "product_name", "product_document_filename" and "product_document".
This "product_document" column store whole file as binary. so that we need to set it's datatype ""varbinary(MAX)". We are inserting uploaded file name in "product_document_filename" column whose datatype is nvarchar(1000).In this example we take product name as textbox and for Upload file we take FileUpload control.

Monday, 27 August 2012

There is always a need to parse and create valid URL.
We can achieve this using "Uri" class's "TryCreate" method.

Here is example for this.
In this example we take on URL string and parse that and create URI object from that.
If URL format in invalid than URL is not parsed and URI object is not created.
This method try to possible all way to create URI object.

If you do not specify "Scheme" in URL string than also this method create object but if you access "Scheme" property of this created object it will return error, so you need to handle this error. This is same for other properties also.

Saturday, 25 August 2012

You can make textbox disabled. Means do not allow enter or change text into textbox.
You can do this with the help of "Enabled" property. You need to set Enabled=false to make text box disabled.By default text box is enabled.

Here is example for this.
In this example we have one text box and we make that textbox disabled.
You can set this property from aspx side or from Coding side.

Friday, 24 August 2012

There are situation in multi line textbox where you want to wrapping text in textbox and sometimes you not want to wrap text , that text type as it is.
We can achieve this using "Wrap" property.By Default it is true.

In textbox Wrapping means if you entered text more than width of the textbox it will automatically set cursor into next line. In unwrapping if you entered text more than width of the textbox there is one horizontal scrollbar appear below the textbox and your line is continued.

Here is example for this.
In this example you can see that we take two text boxes one is "Product Description" and Second is "Product Meta Description". And both this text boxes are multiline textbox.
In "Product Description" textbox we allow warp text in this textbox.
And in "Product Meta Description" textbox we do not allow wraping in text.

You can see in output that in "Product Description" textbox new line created without pressing enter or return key. In "Product Meta Description" textbox scrollbar is appear when entered more data.

Thursday, 23 August 2012

There is always a need of application to set maximum length allowed in textbox.
In this article we demonstrate both single line textbox and multi line textbox max length validation.
There is a different way to set maximum characters allowed for single line textbox and multiline textbox.You can set "MaxLength" property for single line textbox.

For multiline textbox "MaxLength" does not supported.

So for Multi Line textbox we need some tricky approch. We can use javascript , JQuery or server side checking. Here we are going to use .Net Validator controls. We are using "RegularExpressionValidator" control.

Here is example for this.

In this example we are taking two textbox one is for ProdutTitle which is singleline textbox and other is for ProductDescription which is MultiLine textbox. For ProductTitle we are using MaxLength property to set maximum allowed characters. Which is 20 characters.
For ProductDescription we are using "RegularExpressionValidator" control to set maximum allowed characters. Which is 50 characters.
We are using Regex expression for validation.
The "RegularExpressionValidator" control display error message after typing in textbox focus or cursor moved out from textbox.
You can also make server side validation check for regular expression validation using "Page.Validate()" Method and "Page.IsValid" property.

In output you can see that if more than maximum allowed characters type in textbox it will give error message. You can also make you customize message.

Wednesday, 22 August 2012

You can write or insert entry into the Windows event log using the static methods of the EventLog class, or also you can create an EventLog object. But you must decide in which event source you want to write your log entry.The event source is a string that uniquely identifies your application.

By default, there is three event log sources Application, System, and Security. Normally you will write into the Application event source, but you also specify you custom log. You do not need to explicitly create a custom log but when you register an event source against a log, if the specified log doesn’t exist, it’s created automatically.

You can also set type of log entry i.e Error, FailureAudit, Information,SuccessAudit, and Warning. Which you need to set in EventLog class's method.

Here is example for this
In this example we write one event entry in "Application" log section in windows event log.We are using "EventLog.WriteEntry" Method.

Tuesday, 21 August 2012

For security purpose there is a need to convert plain string in cryptographic string or encrypt password, so hackers did not understand the password.
You can create cryptographic string using seven different algorithms that provided in .Net Framework.This is mainly use in encrypt a password and store.
This encrypted password is do not convert back to original string.

Now if you want to compare password for authentication at that time you need to encrypt entered password at that time of login and check that encrypted string with already stored encrypted string.Same input string generate same encrypted hash code.

Here is example for this.
In this Example we use MD5 algorithm. Means Encrypt password using MD5 algorithm.

Monday, 20 August 2012

Hashing algorithms are one way cryptographic functions that accept plain text of any length and generate a numeric value. These are one-way because it’s almost impossible to get the original plain text from the hash code. Hashing algorithms are useful for encrypt the password.

Use the same hashing algorithm to a specific text always generates the same hash code. This makes hash codes useful for check if two blocks of plain text are the same.
Generating hash codes in the .NET Framework is possbile.

The abstract class "HashAlgorithm" provides a base from which all hashing algorithm implementations.
The .NET Framework has the seven hashing algorithm implementations.
This class is in theSystem.Security.Cryptography namespace.

Saturday, 18 August 2012

You can make textbox read only. Means do not allow type text in textbox and also is there is any text predefined in that you are not able to change that text.
You can do this with the help of "ReadOnly" property. You can set ReadOnly=true to make textbox read only.By default text box is not readonly.

Here is example for this.
In this example we have one text box and we make that textbox readonly.
You can set this property from aspx side or from Coding side.
In this example we are showing from both side

defaultRedirect : Specifies URL to which page should be redirected if an error occurs. This setting is optional.

mode : Specifies the status of the custom errors is enabled, disabled, or shown only to remote machines. The values are On, Off, and RemoteOnly. "On" means that the custom errors are enabled. "Off" means that the custom errors are disabled. "RemoteOnly" means that the custom errors are shown only to remote clients.

customErrors : The "<customErrors>" section has multiple <error> sub-elements that are used to define custom errors. Each <error> sub-element can has a statusCode attribute and a URL.

Here is example for this.
In this example we forcefully raise error from code and after error raise page is redirected to our custom "ErrorPage.aspx". In this example we raise error using numeric value divide by zero.
Here you have to add one aspx page into your .net application.
We specify settings in "web.config" file.You have to put "<customErrors>" section in "<system.web>" section.

Thursday, 16 August 2012

This error occurred due to cast or convert or assign NULL value cell of data row or data table.

Error Reason :

Here is example for this.
We have datatable. This data table contains value of cusutomers, thsi data table has three columns.Columns are customer_id, customer_firstname and customer_lastname.Sometimes customer_lastname column has DBNULL value and we directly assign this value to string variable at that time this error occured.

Something like this.
dtCustomer.rows[0]["customer_lastname "] has DBNULL value. and we are assigning this value to variable like this.

Before assigning or converting this DBNULL value we have make check for this.
We can make check with "System.DBNull.Value" value. We compare both "cell" and "System.DBNull.Value" value if both are same so we do not assign or convert that value. We simply use default value.

Thursday, 9 August 2012

You are able to reading event log from .Net application.
We are using "EventLog" class of "System.Diagnostics" namespace.There are three categories of event log available Application, Security and System.

Here is example for this.
In this example we take one combobox, this combobox contains three values "Application", "Security" and "System". You can select any one category to read event log and display it's result in grid.

Wednesday, 8 August 2012

Every ASP.NET server includes a number of configuration files, such as the machine.config , web.config file. This file is installed as a default .NET Framework installation. You can find "machine.config" file and and other server-specific configuration files in "C:\Windows\Microsoft.NET\Framework\v4.xxxxxx" folder. They have the default settings for ASP.NET Web applications on the server.

List of the server-wide configuration files :

machine.config

machine.config.comments

machine.config.default

web.config

web.config.comments

web.config.default

web_hightrust.config

web_hightrust.config.default

web_lowtrust.config

web_lowtrust.config.default

web_mediumtrust.config

web_mediumtrust.config.default

web_minimaltrust.config

web_minimaltrust.config.default

.Net Framework install machine.config , machine.config.default and machine.config.comments files.The machine.config.default file is a backup of the machine.config file. If you want to the factory setting of machine.config file, you simply copy the settings from the machine.config.default to the machine.config file.

The machine.config.comments file contains a description for each configuration section and explicit settings for the most commonly used values. machine.config.default and machine.config.comment files are not used by the .NET Framework runtime, they are installed in case you want to revert to default factory settings.

You can find a root - level web.config file within the same CONFIG folder as the machine.config.By default, ASP.NET Web applications run under a full trust setting. You can see this setting at the <securityPolicy> and <trust> sections in the root - level web.config file.

Note : Let us know if any additional things you know about this topic. We will update our article accordingly that.

Monday, 6 August 2012

We can get all SQL server instances of our Local Network (LAN).
We are using GetDataSources method of the System.Data.Sql.SqlDataSourceEnumerator class.
We are using System.Data.Sql.SqlDataSourceEnumerator.Instance static property and call it's GetDataSources Method.GetDataSources Method returns DataTable , this datatable contains ServerName, InstanceName, IsClustered and Version columns.

ServerName : Name of the server where the SQL Server instance is hosted

InstanceName : Name of the SQL Server instance. the string is empty if the SQL Server is the default instance

IsClustered : Indicate that whether the SQL Server instance is part of a cluster

Version : Version of the SQL Server instance

Here is example for this.
In this Example it will list out all sql server instance accessible and visible on the network.