Monday, May 14, 2012

What is Interface and how to use it in asp.net

In object oriented programming the code can be defined under another container known as interface.which can be defined with only abstract methods in it.Inheritance is organised as implementation and interface inheritance .Implementation inheritance is achieved through classes which is always single.
Interface inheritance is achieved through an interface which is multiple .Multiple inheritance is supported through interfaces under java and .net languages
[<modifiers>]interface<name>
{
<abstract members>;
}
-->An interface cannot contain variables in it
-->members of an interface by default public
-->method under interface are by default abstract
-->while implementing the methods of an interface under the class no need of using the override keyword
-->An interface can inherit another interface


Interface Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
class Program:bhaskar1,bhaskar2
{
static void Main(string[] args)
{
Console.WriteLine("Create my first interface");
Program p = new Program();
p.printbhaskar1();
p.printbhaskar2();
}
public void printbhaskar1()
{
Console.WriteLine("FirstMethod Implementation");
}
public void printbhaskar2()
{
Console.WriteLine("Second Method Implementation");
Console.ReadKey();
}
}
interface bhaskar1
{
void printbhaskar1();
}
interface bhaskar2
{
void printbhaskar2();
}
}

Saturday, May 12, 2012

convert uppercase to lowercase in xslt


Here i will show how to translate the uppercase letter to lowercase using xslt.For this i have created a variables ,which are used in this conversion process.Then the result will be assigned to one parameter which is "purl".
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="CurrentUrl"></xsl:param>
    <xsl:param name="PUrl"></xsl:param>
    <xsl:variable name="lowercletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
    <xsl:variable name="uppercletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable> 
    <xsl:template match="/">
        <xsl:for-each select="/Item/Menu">
            <xsl:if test="$PUrl = translate(Link, $uppercletters, $lowercletters)">
                <h1>
                    <xsl:value-of select="item"/>
                </h1>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Friday, May 11, 2012

how to create object in asp.net

We can create the object of a class from any where either in the same class or from the other class.When created with in  a class we created under main method.As it was the entry point
Syntax to create an object
<class> <obj>=new <class>([<arglist>])
program p=new program()
p.method1();
p.method2();
p.method3();
Here program p is object ,
program p=new program() is for allocate memory

Thursday, May 10, 2012

How to dynamically add CSS file in ASP.NET

Here i will show how to add css file to page in code behind.For this i have created a custom HTML link control and assign the styles sheet URL ,set remaining properties. We can see one more thing here is creating HTML control dynamically
In aspx page:
<link href="~/styles.css" rel="Stylesheet" type="text/css" />
Codebehind:

       HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("href", "~/Styles.css");
this.Header.Controls.Add(link);
This.Header is used to add the link to aspx page

Wednesday, May 9, 2012

Convert a Generic List into a Datatable in asp.net

According to my application requirement ,i need to bind the data to data table from List in asp.net using C#.net.For this i just create table dynamically then bind the data using loop iteration on list.
In the below we can see two methods which are CreateDataTable which is used to create a data table and AddDataToTable which is used to bind the data .
public DataTable converttoTable(List<string> list, string tableName)
{
DataTable table;
table = CreateDataTable(); 
if (list.Count > 0)
{
foreach (string item in list)
{
AddDataToTable(item, table);
}
}
return table;
}
This function will execute when the items are added to data table.
private void AddDataToTable(string rowItem, DataTable myTable)
{
try
{
DataRow row;
row = myTable.NewRow();
string[] itemStringArray = SplitByString(rowItem, "[]");
row["Iame"] = itemStringArray[0];
row["Id"] = itemStringArray[1];
row["Address"] = itemStringArray[2] == "" ? "0" : itemStringArray[2];
row["Year"] = itemStringArray[3];
row["salary"] = itemStringArray[4];
myTable.Rows.Add(row);
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
//create a table dynamically
private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();
DataColumn myDataColumn;

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Name";
myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.Int32");
myDataColumn.ColumnName = "Id";
myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Address";
myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.Int32");
myDataColumn.ColumnName = "Year";
myDataTable.Columns.Add(myDataColumn);           

myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.Int32");
myDataColumn.ColumnName = "Salary";
myDataTable.Columns.Add(myDataColumn);

return myDataTable;
}

Bel