0% found this document useful (0 votes)
52 views

How Will You Fill The Gridview by Using Datatable Object at Runtime?

The document describes how to fill a GridView with data from a DataTable object that is populated at runtime in C#. It creates a DataTable with columns for ID and ItemName, adds 5 rows with sample data, and sets the GridView's DataSource to the DataTable before calling DataBind().

Uploaded by

khushboo_khanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

How Will You Fill The Gridview by Using Datatable Object at Runtime?

The document describes how to fill a GridView with data from a DataTable object that is populated at runtime in C#. It creates a DataTable with columns for ID and ItemName, adds 5 rows with sample data, and sets the GridView's DataSource to the DataTable before calling DataBind().

Uploaded by

khushboo_khanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

How will you fill the GridView by using DataTable object at

runtime?
using System;
using System.Data;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("Employee"); // Create the table object
DataColumn dc = new DataColumn();
DataRow dr;
dc.ColumnName = "ID"; //Heading of the coloumn
dc.DataType = Type.GetType("System.Int32"); //Set the type of ID as Integer
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "ItemName";
dc.DataType = Type.GetType("System.String"); //Set the type of ItemName as String
dt.Columns.Add(dc);
for (int i = 0; i <= 4; i++) //This code will create 5 new rows
{
dr = dt.NewRow();
dr["id"] = i;
dr["ItemName"] = "Item " + i;
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

Output of the above example:

Describe DataReader object of ADO.NET with example.


- The DataReader object is a forward-only and read only object
- It is simple and fast compare to dataset.
- It provides connection oriented environment.
- It needs explicit open and close the connection.
- DataReader object provides the read() method for reading the records. read() method
returns Boolean type.
- DataReader object cannot initialize directly, you must use ExecuteReader() method to
initialize this object.
Example:
using System;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
public partial class CareerRide : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlDataReader reader;
SqlConnection MyConnection = new SqlConnection("Data Source=name of your

datasource;Initial Catalog=Employee;Integrated Security=True");


SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM emp";
cmd.CommandType = CommandType.Text;
cmd.Connection = MyConnection; MyConnection.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
GridView1.DataSource = reader;
GridView1.DataBind();
cmd.Dispose();
MyConnection.Dispose();
}
}
}

What is Serialization and De-Serialization in .Net? How can we


serialize the DataSet object?
Serialization is the process of converting an object into stream of bytes that can be stored
and transmitted over network. We can store this data into a file, database or Cache object.
De-Serialization is the reverse process of serialization. By using de-serialization we can get
the original object that is previously serialized. Following are the important namespaces for
Serialization in .NET.
- System.Runtime.Serialization namespace.
- System.Runtime.Serialization.Formatters.Binary
- System.Xml.Serialization
The main advantage of serialization is that we can transmit data across the network in a
cross-platform environment and we can save in a persistent or non-persistent storage
medium. Serialization can be the following types:
- Binary Serialization
- SOAP Serialization
- XML Serialization
- Custom Serialization

How can you serialize the DataSet ? Explain with example.


using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml.Serialization;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=Your data source name;Initial
Catalog=Demo;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter("select * from employee", con);
DataSet ds = new DataSet(); da.Fill(ds);
FileStream fileObj = new FileStream("C:\\abc.xml", FileMode.Create);
XmlSerializer serObj = new XmlSerializer(typeof(DataSet));
serObj.Serialize(fileObj, ds); fileObj.Close();
}
}
In the above given example the database name is Demo and table name is employee. The
data will be serialized in abc.xml file..

Describe the command object and its method.


After successful connection with database you must execute some sql query for
manipulation of data or selecting the data. This job is done by command object. If you are
using SQL Server as database then SqlCommand class will be used. It executes SQL
statements and Stored Procedures against the data source specified in the Connection
Object. It requires an instance of a Connection Object for executing the SQL statements.
ExecuteReader :

This method works on select SQL query. It returns the DataReader object. Use DataReader
read () method to retrieve the rows.
ExecuteScalar :
This method returns single value. Its return type is Object
ExecuteNonQuery :
If you are using Insert, Update or Delete SQL statement then use this method. Its return
type is Integer (The number of affected records).
using System;
using System.Data;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=Your data source name;Initial
Catalog=Demo;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from employee";
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
DropDownList1.Items.Add(reader[1].ToString());
}
}
else
{
Label1.Text="No rows found.";
}
reader.Close();
con.Close();

}
}
In the above given example a DropdownList has been taken that is filled by second column
of employee table.

What is the difference between DataSet and DataReader?


The following are the main difference between DataSet and DataReader.
DataSet :
- Consumer Object
- Provides Disconnected mode
- Forward and backward scanning of data
- Slower access to data
- Can store multiple table simultaneously
- Read/Write access
DataReader :
- Provider Object
- Provides Connected mode
- Forward-only cursor
- Faster access to data
- Supports a single table based on a single SQL query
- Read Only access

You might also like