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.

Wednesday, August 11, 2010

Finding Row Index in GridView RowCommand Event

GridView RowCommand Event


The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.

The GridView control also raises other specialized events when certain buttons are clicked; means buttons with the CommandName property set to "Delete", "Update", "Page" etc…

When using one of these buttons, you should consider handling one of the specialized events provided by the control; such as RowDeleted or RowDeleting.

We will discuss more about these specialized events later.

protected void gvProjects_RowCommand(object sender, GridViewCommandEventArgs e)
{


}

The above example shows a sample RowCommand event handling method.

A GridViewCommandEventArgs object is passed to the event-handling method, which allows you to determine the command name and command argument of the button clicked. To determine the command name and command argument, use the CommandName and CommandArgument properties, respectively. You can also access the button control that raised the event by using the CommandSource property.

To find index of row, which caused this event, we can use any of the following method:

1. We can use e.CommandArgument property to find row index. ( It will work only if we didn’t set the CommandArgument property of button which we clicked)

Eg:
int index = Convert.ToInt32(e.CommandArgument);

2. Second way is, we can find index from e.CommandSource property. See the following example to know more about it

Eg:
LinkButton lbtn = (LinkButton)e.CommandSource; // get event caused button
GridViewRow gvrow = (GridViewRow)lbtn.NamingContainer; // get grid row contains event caused button
int index = gvrow.RowIndex;


Hope It will help you to handle GridView's RowCommand Event.

Tuesday, August 10, 2010

Creating a Database

Database


A database is a collection of information that is organized so that it can easily be accessed, managed, and updated.

create a database:

syntax:  create database database_name

Eg: CREATE DATABASE TEST

database_name is the name of the database. The maximum size of a database name is 128 characters.
SQL server has two operating system file types:

1. Data files (has extension .mdf)

2. Log files ( has extension .ldf)

Data files contain data and objects such as tables and indexes. Log files contain transaction log for recovering the database transactions.

Specifying the data file and log file

Eg:

CREATE DATABASE DBTEST
ON
(
NAME='DBTESTDATA',
FILENAME='E:\jobin\database\DBTESTDATA.mdf',
SIZE=10mb,
MAXSIZE=100mb,
FILEGROWTH=10mb
)


LOG ON
(
NAME='DBTESTLOG',
FILENAME='E:\jobin\database\DBTESTLOG.ldf',
SIZE=5mb,
MAXSIZE=50mb,
FILEGROWTH=5mb
)