Monday, August 16, 2010

Jagged array in C#

Jagged array:


A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays".

Declaration:       int[][] array;


Initialization:      array=new int[3][];
                        array[0]=new int[2];
                        array[1]=new int[5];
                        array[2]=new int[3];

 
Eg:
 
using System;

class jaggedarray
{
int[][] ja;


public void Accept()
{
Console.WriteLine("Enter the size of jagged array :");
int jsize = Convert.ToInt32(Console.ReadLine());
ja = new int[jsize][];
for (int i = 0; i < jsize; i++)
{
Console.WriteLine("Enter size of Single dimension array ");
int ssize = Convert.ToInt32(Console.ReadLine());
ja[i] = new int[ssize];
Console.WriteLine("Enter {0} elements ", ssize);
for (int j = 0; j < ssize; j++)
{
ja[i][j] = Convert.ToInt32(Console.ReadLine());
}
}
}


public void Display()
{
Console.WriteLine("Jagged Array Is :");
foreach (int[] a in ja)
{
foreach (int n in a)
{
Console.Write(n + "\t");
}
Console.WriteLine();
}
}
}


class Mainclass
{
public static void Main()
{
jaggedarray obj = new jaggedarray();
obj.Accept();
obj.Display();
}
}

Performing site navigation in Asp.Net

Performing site navigation


Page navigation is the process of moving between one actual page of your Web site and another.
There are many ways to navigate from one page to another in ASP.NET.

Client-side navigation: Client-side code or markup allows a user to request a new Web page. Your client code or markup requests a new Web page in response to a client-side event, such as clicking a hyperlink or executing JavaScript as part of a button click.

Cross-page posting: A control and form are configured to PostBack to a different Web page than the one that made the original request.

Client-side browser redirect: Server-side code sends a message to the browser, informing the browser to request a different Web page from the server.

Server-side transfer: Server-side code transfers control of a request to a different Web page.

Friday, August 13, 2010

Read & Write operations using Console class

Console Class:


In this class we use two important methods named WriteLine() and ReadLine(). This class contained in the System namespace.

Eg: Print a message

class WriteString
{
public static void Main()
{
   System.Console.WriteLine("Welcome to C#.")
}
}

Eg: read a string

using System;
class WriteString
{
public static void Main()
{
   Console.WriteLine("Enter a string ");
   string str = Console.ReadLine();
   Console.WriteLine("You entered = " + str);
}
}

WriteLine() pumps a text string to the output stream. ReadLine() allows you to receive information from the input stream. The ReadLine(0 method returns a string value.

Thursday, August 12, 2010

Attaching detaching Databases

Attaching detaching Databases


At times, you may want to move an entire database- including all of its objects data, and log files- to another SQL server machine. To do this, SQL server enables attaching and detaching databases.

Eg: Detach

SP_DETACH_DB 'DBTEST'

Eg: Attach

SP_ATTACH_DB 'DBTEST','E:\jobin\database\DBTESTDATA.mdf','E:\jobin\database\DBTESTLOG.ldf'

Reserved CommandName values

GridView Control's Reserved CommandName values and its Description

Cancel :  Cancels an edit operation and returns the GridView control to read-only mode. Raises the    RowCancelingEdit event.


Delete : Deletes the current record. Raises the RowDeleting and RowDeleted events

Edit : Puts the current record in edit mode. Raises the RowEditing event,

Page : Performs a paging operation. Sets the CommandArgument property of the button to "First", "Last", "Next", "Prev", or a page number to specify the type of paging operation to perform. Raises the PageIndexChanging and PageIndexChanged events

Select: Selects the current record. Raises the SelectedIndexChanging and SelectedIndexChanged events.

Sort: Sorts the GridView control. Raises the Sorting and Sorted events.

Update : Updates the current record in the data source. Raises the RowUpdating and RowUpdated events.

Custom Sorting with GridView Control | Handling GridView Sorting Event

Custom Sorting with GridView Control, Handling GridView Sorting Event

Bind a grid, and Set SortExpresion property for the particular field to be sortable (Eg, BoundField).
In the following example coulmn name is assigned to SortExpression property. I have a table with column with country; I need to sort records in ascending and descending order based on the country.

In the following example you can see three methods:

1. GridView1_Sorting :  GridView's Sorting event handling method.

2. Sort : implementation of sort logic

3. ManageViewState : To keep current sorting order.


#region Sort




protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
   Sort(e.SortExpression);
}


void Sort(string column)
{


switch(column)
{
case "Country":


if (ViewState[column] == null)
{
reportsSorted = (from r in report orderby r.Country ascending select r).ToList();
ManageViewState(column,true);
}

else
{
reportsSorted = (from r in report orderby r.Country descending select r).ToList();
ManageViewState(column, false);
}
break;


}


BindGrid(reportsSorted);
}



void ManageViewState(string key, bool Tokeep)
{
if (Tokeep)
    ViewState[key] = true;
else if(ViewState[key]!=null)
   ViewState.Remove(key);


}
#endregion


Have a look at above code and try to work out it.