Showing posts with label vb .net programming. Show all posts
Showing posts with label vb .net programming. Show all posts

Wednesday, 2 July 2014

It is very easy to remove string's leading and trailing white spaces using Regex.

 The Regex is "^\s+|\s+$".

Here is example of this.

In this example we are taking on string which has leading and trailing white spaces. We can remove this spaces using Regex.

C#. Net Example :
string strData = "            This string contains leading and trailing white spaces             ";
strData = Regex.Replace(strData, @"^\s+|\s+$", "");
Response.Write(strData);

VB.Net Examples :
Dim strData As String = "            This string contains leading and trailing white spaces             "
strData = Regex.Replace(strData, "^\s+|\s+$", "")
Response.Write(strData)

  
Click here for other post regarding Remove only leading white spaces from string using Regex with C#.Net and VB.Net example


Click here for other post regarding Remove only trailing white spaces from string using Regex with C#.Net and VB.Net example


Wednesday, 25 June 2014

It is very easy to remove string's trailing white space.

We can achieve using Regex. The Regex is "[ \t]+$".

Here is example of this.

In this example we are taking on string which has trailing white space. We can remove this spaces using Regex.

C#. Net Example :
string strData = "This string contains trailing white spaces             ";
strData = Regex.Replace(strData, "[ \t]+$", "");
Response.Write(strData);

VB.Net Examples :
Dim strData As String = "This string contains trailing white spaces             "
strData = Regex.Replace(strData, "[ \t]+$", "")
Response.Write(strData)


Click here for other post regarding Remove only leading white spaces from string using Regex with C#.Net and VB.Net example

Click here for other post regarding Remove leading and trailing white spaces from string using Regex with C#.Net and VB.Net example.  


Wednesday, 18 June 2014

It is very easy to remove string's leading white space.

We can achieve using Regex. The Regex is "^[ \t]+".

Here is example of this.
In this example we are taking on string which has leading white space. We can remove this spaces using Regex.

C#. Net Example :
string strData = "   This string contains leading white spaces";
strData=Regex.Replace(strData, "^[ \t]+", "");
Response.Write(strData);

VB.Net Examples :
Dim strData As String = "   This string contains leading white spaces"
strData = Regex.Replace(strData, "^[ \t]+", "")
Response.Write(strData)


Click here for other post regarding Remove only trailing white spaces from string using Regex with C#.Net and VB.Net example

Click here for other post regarding Remove leading and trailing white spaces from string using Regex with C#.Net and VB.Net example.  
 

Wednesday, 26 March 2014

You can fill DataTable from another DataTable using LINQ after doing LINQ operation.

This would be useful in condition like you need to perform some operation on DataTable using LINQ and get back result into another 'DataTable'. Means, you can filter and projection on one DataTable and get result in another DataTable.

We can achieve this by using 'CopyToDataTable' method of LINQ query result variable.

Here is example for this.
In this example we are taking one DataTable which contains product information like ID, name, etc. Now we get only those products which ID is greater than one by LINQ operation on this table and get result in another table using 'CopyToDataTable' method of LINQ query result variable.

Tuesday, 4 March 2014

You can create XML file from another file with selected elements from source XML file using LINQ. In this, we query the source XML file using LINQ and write or create new XML file.

Here is example for this. 
In this example we take one "Book.xml" file. In this file we have element like Title, Pages, ISBN and Auther. Now we are querying the "Book.xml" file to get only Title and ISBN of book and create another XML with data that contains only Title and ISBN element.

Wednesday, 12 February 2014

You can fill List of object from DataTable using LINQ. There are many situations where you need to get data from DB into data table and after that from data table to proper class object. We can do this easily with LINQ.

Here is example for this.
In this example we take one DataTable object which contains two columns 'product_id' and 'product_name'. This DataTable has many rows. Now we want to convert DataTable object into list of class object. We have one Class 'clsProduct' which has two property 'product_id' and 'product_name'. Now we transform DataTable object data in to list of clsProduct class object using LINQ.

Thursday, 16 January 2014

You can attach events like 'onChanging' and 'onChanged' events on XML document object. These events raise when you 'ADD' or 'REMOVE' XML element from XML Document object.

First 'onChanging' event raise after that 'onChanged' event raise. In this event we get 'XObjectChangeEventArgs' class object. This object give us property like 'ObjectChange'. In this property we can know that which method is performed.

When we call 'Add' method we get 'Add' value in 'ObjectChange' property and when we call 'Remove' method we get 'Remove' value in 'ObjectChange' property.

Here is example on this.

Friday, 20 December 2013

You can validate the data of XML file and identify XML elements which are failed to validate. We can easily achieve this using LINQ.

Here is example for this.
In this example we take one 'customer.xml' file. In this file we have 'name' and 'phone' element. We need to validate phone number with REGEX and display it's validate result either it is true or false.

Wednesday, 11 December 2013

Using LINQ you can check that attribute exist in element or not. There is property called "HasAttributes" of XElement class to check existence of attribute.

Here is example for this.

In this example we take one 'customers.xml' file which contains customers information like name, id, etc. Now we check that does "customer" element has 'id' attribute or not. In XML there is one customer 'David' which does not have 'id' attribute. Now we iterate each 'customer' element and display that it element has attribute or not.

Wednesday, 20 November 2013


 XML file contains various types of data like string, integer, date, etc. Now if we want to get data from XML file order by some attribute which contains dates at that time we need to do some special things if we are not doing that at that time data is not sorted date wise it will treat as normal string sorting. We can do date wise sorting. 

Here are examples for this.
In this example we are taking on 'Books.xml' file which contains books details in this XML file for each book element we are one "ReleaseDate" element which contains date value. Now we take that XML file in XMLDocument object and sort on "ReleaseDate" attribute by converting that string date into date datatype.

Wednesday, 16 October 2013

You can query objects using SelectMany LINQ method and get result according to your given criteria. You can also query more than one criteria.

Here is example for this.

In this example we take "books.xml". Now we query on element "author" whose sub element is 'firstname' and 'lastname' and we only want list of authors whose name is either 'Pratik' or 'David'.

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.

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, 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, 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.

Monday, 29 July 2013

There are situations where some time we want to get root element of XML file or document. We can get this root element and root element name very easily. We are using "Root" property of "XDocument" class.

Here is example for this.
In this example we take on "BooksList.xml" file and we display its root element name.

C#. Net Example :
        XElement objRootElement = XDocument.Load(Server.MapPath("BooksList.xml")).Root;
        Response.Write("<b>Name of root element is : </b>" + objRootElement.Name);

VB.Net Examples :
        Dim objRootElement As XElement = XDocument.Load(Server.MapPath("BooksList.xml")).Root
        Response.Write("<b>Name of root element is : </b>" & objRootElement.Name.ToString())

Output :


Below are the books that you would like :

Monday, 15 July 2013

We can get specific attributes values from XML file or document or object using LINQ. We are using "Elements" and "Attributes" methods of XML document object to get attributes values.

Here is example for this.
In this example we take one "BooksList.xml" file. This file contains books related details and each book has ISBN number which appears as attribute in "<book>" node. We retrieve this attribute using LINQ and display on screen.

Saturday, 29 June 2013

 Click Here to Download FactoryPatternSampleWithCSharp.zip C# Example.
 Click Here to Download FactoryPatternSampleWithVBNET.zip VB.Net Example.

Factory Design Pattern is type of creational patterns. We can architect software with the help of factory design pattern. With the help of factory pattern we can centralize creations of objects and also reduce object creation logic at the client side.

Let's see some example on this.
We take booklet display example. You have functionality like display booklet on screen in two ways one is with header and footer and other is plain without header and footer.

In traditional approach you might take two classes "BookletWithHeaderFooter" and "BookletPlain" and create objects depending on your requirement on client side like below code.

Tuesday, 18 June 2013

We can Create or Generate XML file or document programmatically using XDocument, XElement and XAttribute class. We can use infinite level of XML element. We can also create "web.config" file programmatically.

Here is example for this.
In this example we create sample "web.config" file using XElement and XAttribute class and display that generated XML in textbox. We are using ".ToString" method of XDocument object to get XML string.

ASPX Code :
    <asp:TextBox runat="server" ID="txtXML" TextMode="MultiLine" Rows="20" Columns="110"></asp:TextBox>


C#. Net Example :
        XDocument myDocument = new XDocument(
                                  new XElement("configuration",
                                    new XElement("system.web",
                                      new XElement("membership",
                                        new XElement("providers",
                                          new XElement("add",
                                            new XAttribute("name","WebAdminMembershipProvider"),
                                            new XAttribute("type","System.Web.Administration.WebAdminMembershipProvider")))),
                                      new XElement("httpModules",
                                        new XElement("add",
                                          new XAttribute("name","WebAdminModule"),
                                          new XAttribute("type","System.Web.Administration.WebAdminModule"))),
                                      new XElement("authentication",
                                        new XAttribute("mode", "Windows")),
                                      new XElement("authorization",
                                        new XElement("deny",
                                          new XAttribute("users", "?"))),
                                      new XElement("identity",
                                        new XAttribute("impersonate", "true")),
                                      new XElement("trust",
                                        new XAttribute("level", "full")),
                                      new XElement("pages",
                                        new XAttribute("validationRequest", "true")))));

        txtXML.Text = myDocument.ToString();

VB.Net Examples :
        Dim myDocument As New XDocument(
            New XElement("configuration",
                                    New XElement("system.web",
                                        New XElement("membership",
                                        New XElement("providers",
                                            New XElement("add",
                                            New XAttribute("name", "WebAdminMembershipProvider"),
                                            New XAttribute("type", "System.Web.Administration.WebAdminMembershipProvider")))),
                                        New XElement("httpModules",
                                        New XElement("add",
                                            New XAttribute("name", "WebAdminModule"),
                                            New XAttribute("type", "System.Web.Administration.WebAdminModule"))),
                                        New XElement("authentication",
                                        New XAttribute("mode", "Windows")),
                                        New XElement("authorization",
                                        New XElement("deny",
                                            New XAttribute("users", "?"))),
                                        New XElement("identity",
                                        New XAttribute("impersonate", "true")),
                                        New XElement("trust",
                                        New XAttribute("level", "full")),
                                        New XElement("pages",
                                        New XAttribute("validationRequest", "true")))))

        txtXML.Text = myDocument.ToString()

Output :
Create or Generate XML file programmatically using XElement and XAttribute class
(To view original image , click on image)

Tuesday, 11 June 2013

XML declaration of a XML document or file is very important. We can add XML declaration and create XML element programmatically .
We are using "XDocument" and "XDeclaration" class to add declaration.

Here is example for this.
In this example we create XML document with specified XML declaration and save it into the StringWriter object. You can see in the debug time screenshot.

C#. Net Example :
        XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-16", "Yes"),new XElement("Products"));
        StringWriter sw = new StringWriter();
        doc.Save(sw);
        Response.Write(sw.ToString());

VB.Net Examples :
        Dim doc As New XDocument(New XDeclaration("1.0", "UTF-16", "Yes"), New XElement("Products"))
        Dim sw As New StringWriter()
        doc.Save(sw)
        Response.Write(sw.ToString())

Output :
Add XML declaration programmatically
(To view original image , click on image)