Thursday, 26 September 2013

We can get or retrieve full path of the given XML Node or Element. Means from root element to the given XML Node. We can achieve this  LINQ's extensions methods. We are using 'Descendants', 'AncestorsAndSelf' and 'Reverse' methods.

  • Descendants : Returns filtered collections of the given elements from document or element. For more details Click Here.
  • AncestorsAndSelf : Returns collection of elements that contain this element. For more details Click Here.

Here is C#.Net example and VB.Net example.

Thursday, 19 September 2013

You can check Database Status using SQL query.

Below are the possible status of database.
  • ONLINE : Database is available for access.
  • RESTORING : Primary file group and secondary files are being restored.
  • RECOVERING : Database is being recovered.
  • RECOVERY PENDING : Resource-related error occurred during recovery.
  • OFFLINE : Database is unavailable to access.
  • SUSPECT : One or many primary file group is suspect and may be damaged.
  • EMERGENCY : User has changed the database and set the status to EMERGENCY.

Here is query for this.

Wednesday, 11 September 2013

You can directly get element of particular position or query that element using LINQ's 'Descendants' and 'ElementAt' method.

  • 'Descendants' is a method of XDocument class which accept name of element as string.
  • 'ElementAt' is a method of 'XElement' class which accept integer value of index.

Means you can query the 2nd book element in the XML document or file. You can also call that as positional predicate.

Here is example for this.
In this example we took one XML file 'BooksList.xml'. Now we use 'Descendants' method of 'XDocument' and pass 'book' element and after that, we use 'ElementAt' method and pass '1' so that we can get 2nd element of 'book' from that XML file.


C#. Net Example :
XDocument objDoc = XDocument.Load(Server.MapPath("BooksList.xml"));
XElement objElement = objDoc.Descendants("book").ElementAt(1);

Response.Write("<b>Title Element Value : </b>" + objElement.Element("Title").Value);
Response.Write("<br/>");
Response.Write("<b>ReleaseDate Element Value : </b>" + objElement.Element("ReleaseDate").Value);
Response.Write("<br/>");
Response.Write("<b>Pages Element Value : </b>" + objElement.Element("Pages").Value);

VB.Net Examples :
Dim objDoc As XDocument = XDocument.Load(Server.MapPath("BooksList.xml"))
Dim objElement As XElement = objDoc.Descendants("book").ElementAt(1)

Response.Write("<b>Title Element Value : </b>" + objElement.Element("Title").Value)
Response.Write("<br/>")
Response.Write("<b>ReleaseDate Element Value : </b>" + objElement.Element("ReleaseDate").Value)
Response.Write("<br/>")
Response.Write("<b>Pages Element Value : </b>" + objElement.Element("Pages").Value)

BooksList.xml
<?xml version="1.0" encoding="utf-16"?>
<books>
  <book ISBN="asp1">
    <Title>ASP.NET</Title>
    <ReleaseDate>11/11/2010</ReleaseDate>
    <Pages>200</Pages>
  </book>
  <book ISBN="c#2">
    <Title>C#.NET</Title>
    <ReleaseDate>10/11/2010</ReleaseDate>
    <Pages>500</Pages>
  </book>
</books>


Output :
LINQ positional predicate


Below are the books that you would like :

Monday, 9 September 2013

Click Here to Download UploadAndDownloadFileInMVC.zip Example.
 
In many software application uploading and downloading files are required. There are very easy way to upload and download file in ASP.Net MVC framework.

Here is step by step example of how to implement upload and download functionality in MVC.

In this example we will also demonstrate how to change encoding type of form, how to apply CSS class to "Button" and "ActionLink" control and also How to apply style attribute with value.

STEP 1 : Add Upload View
Here is HTML of Uplod view file.

@using (Html.BeginForm("Index", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
    @Html.AntiForgeryToken() 
     <fieldset>
         <legend>Upload a file</legend>
        @Html.Raw(ViewBag.Message)
            <div class="editor-field">
                    @Html.TextBox("file", "", new { type = "file" }) 

            </div>
            <div class="editor-field">
                <input type="submit" value="Upload File" class="button_example" />
            </div>
         @Html.ActionLink("Go To Download Page","Index","Download",null, new {@class = "button_example",style="float:right"})
    </fieldset> 
} 


Monday, 26 August 2013

You can get Parent element from child element using LINQ method. Some times there are situations when you traverse XML file and you reach at child node and you want to get details of Parent node at that time you can use "Parent" method which will give you parent node as XElement object.

Here is example for this.
In this example we took one "BooksList.xml" file. Now we traverse to child node "Title" and after that, we get its parent node using "Parent" property.

Monday, 19 August 2013

Click Here to MVC_Regular_Expression_ValidationUsingAnnotations.zip Example.

We can do Regular Expression or REGEX validation using Annotations in MVC. We can use "RegularExpression" attribute for validation. In this attribute we specify Regular Expression string.

We can also specify our custom error messages for that we need to set "ErrorMessage" Property in "RegularExpression" attribute.

Here is example for this.

Monday, 12 August 2013

Download AbstractFactoryDesignPatternExampleWithCSharp.zip C# Example.
Download AbstractFactoryDesignPatternExampleWithVB.Net.zip VB.Net Example.


Abstract factory pattern is expanded version of basic factory pattern. In Abstract factory pattern we unite similar type of factory pattern classes in to one unique interface.

Factory classes helps to centralize the creation of classes and types. Abstract factory help bring equability between related factory patterns classes. Which create more simplified interface for the client.

Now let's understand basic details of how to implement abstract factory patterns. We have the factory pattern classes which integrate to a common abstract factory classes through inheritance. Factory classes lay over concrete classes which are derived from common interface.
For instance in below figure we demonstrate "Implementation of abstract factory".

Abstract  Factory Design Pattern
(To view original image , click on image)


Both the concrete classes "ConcreateClass1" and "ConcreateClass2" inherits from one common interface. The client has to only interact with the abstract factory and the common interface from which the concrete classes inherit.