Thursday, January 7, 2010

What is the difference between datatable and dataset?

Dataset
dataset is collection of data table and it is provide interface between database and datatable. dataset consist multiple table. A Dataset is an in-memory representation of a collection of Database objects including tables of a relational database schema. Dataset represents an in-memory cache of data. The dataset contains a collection of Tables, Relations, constraints etc., The dataset contains all the objects as a Collection of objects. For example it contains the Database Tables as DataTable objects. Dataset is a collection of tables, which is used in disconnected architecture. Generally to fill dataset we use fill method of dataadapter.

It can be used for manipulating the data remotely and finally updating the database with the modified data. This way it enables disconnected means of working with data. This improves performance in terms of reducing the number of times a database is accessed for data manipulations.

Example for dataset:
using System.Data.SqlClient;
using System.Data;

string strDBConnection = "server=(local);database=DatabaseName;user id=UserName;password=Pwd;connection reset=false;connection lifetime=5;Trusted_Connection=Yes;"
SqlConnection dbConnection;
dbConnection = new SqlConnection(strDBConnection);

string strSelectSql = "Select * from [DatabaseName].[OwnerName].[TableName] order by FieldName";

//Open the connection
dbConnection.Open();

//Create a command
SqlCommand selectSqlCommand = new SqlCommand(strSelectSql,dbConnection);
SqlDataAdapter sqlData = new SqlDataAdapter(selectSqlCommand);
DataSet dsSelectData = new DataSet();
sqlData.Fill(dsSelectData);


Data Table
Data table is a collection of records that consist the single table. You can get datatable from dataset as follows.

DataTable tbl = DataSet.Tables["Tablename" or Table instance number]

Datatable represents one table of in-memory data. Datatable is a single table. datatable is just like table which has columns and rows. A DataTable is an in-memory representation of data, typically retrieved from a database or XML source.

Monday, December 28, 2009

Grid View Data Format String

DataFormatString property of GridView BoundField allows you to specify the format for number values and display them with different styles. You can display the number value as currency, number, decimal or hexadecimal formats.

List of Formats

1. {0:C} Currency format

2. {0:D} Decimal format

3. {0:E} Exponential format

4. {0:F} Fixed format

5. {0:G} General format

6. {0:N} Number format

7. {0:X} Hexadecimal format

8. {0:C2} Currency with 2 decimal places

9. {0:00.00} Number with 2 decimal places

10. {0:000} Number with 2 decimal places

You can also set the GridView Date Formats such as: {0:MM-dd-yyyy} to display the date with month number, day and year, {0:MMM dd, yyyy} to display the date with first three letters of month name, day and year. MMM dd, yyyy, MM-dd-yyyy, MM/dd/yyyy, dd/MM/yyyyy etc as per your requirement.

{0:MM-dd-yyyy HH:mm ss tt} - To display the time format along with date you can use HH for 24Hour, hh for hour, mm for minutes, ss for seconds and tt for AM/PM.