ASP.NET Session 13 14

Post on 27-May-2015

297 views 0 download

Tags:

Transcript of ASP.NET Session 13 14

GridView

Session 13-14

Objectives

• Introduction of DataSet• Gridview control• Several operation with GridView

DataSet Object

• Replaces the ADO Recordset

• Represents a cache of data that contains tables, columns, relationships, and constraints, just like a database

• Regardless of where the source data comes from, data can all be placed into DataSet objects

• Tracks changes that are made to the data it holds before updating the source data.

As revolutionary as a DataSet might be, it is not the best choice in every situation. Often, using a DataSet might not be appropriate; instead, using a DataReader might be better.

Because the DataSet is a disconnected copy of the data, you can work with the same records repeatedly without having to go back to the data store. This capability can greatly increase performance and lessen the load upon the server.

Gridview

• The GridView control is a powerful data grid control that allows you to display an entire collection of data,add sorting and paging, and perform inline editing.

• GridView by dragging the control onto the design surface of an ASP.NET Web page.

• In column definition TemplateField &ItemTemplate we will insert any control .

• Customizing Columns in the GridView:

If you have your grid configured to automatically generate columns based on the bound data source,

the grid creates fields for each public property exposed by the data source.

Field Control DescriptionBoundField Displays the value of a field in a data source. This is

the default column type of the GridView control.

CheckBoxField Displays a check box for each item in the GridView control. This column field type is commonly used to display fields with a Boolean value.

HyperLinkField Displays the value of a field in a data source as a hyperlink. This column field type allows you to bind a second field to the hyperlink’s URL.

ButtonField Displays a command button for each item in the GridView control. This allows you to create a column of custom button controls, such as an Add or Remove button.

ImageField Automatically displays an image when the data in the field represents an image.

Edit,Update,Delete Operation in GridView

Event Generate Code• GridView_course.RowDeleting += new

GridViewDeleteEventHandler(GridView_course_RowDeleting);

• GridView_course.RowEditing += new GridViewEditEventHandler(GridView_course_RowEditing);

• GridView_course.RowUpdating += new GridViewUpdateEventHandler(GridView_course_RowUpdating);

• GridView_course.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView_course_RowCancelingEdit);

RowCancelingEdit Event Execution Code

• void GridView_course_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

• {• GridView_course.EditIndex = -1;• DataSet ds_Row = new DataSet();• ds_Row = dcon.Data_inventer("Select Subject

as Subject,courseOffer as Courses_Offer from Course order by Subject");

• GridView_course.DataSource = ds_Row;• GridView_course.DataBind();• }

RowEditing Event Execution Code

• void GridView_course_RowEditing(object sender, GridViewEditEventArgs e)

• {• GridView_course.EditIndex = e.NewEditIndex;• DataSet ds_Row = new DataSet();• ds_Row = dcon.Data_inventer("Select Subject as

Subject,courseOffer as Courses_Offer from Course order by Subject");

• GridView_course.DataSource = ds_Row;• GridView_course.DataBind();• • }

RowDeleting Event Execution Code

• void GridView_course_RowDeleting(object sender, GridViewDeleteEventArgs e)

• {• dcon.inupdl("delete from Course where Subject='" +

GridView_course.Rows[e.RowIndex].Cells[3].Text + "'");• DataSet ds_Row = new DataSet();• ds_Row = dcon.Data_inventer("Select Subject as

Subject,courseOffer as Courses_Offer from Course order by Subject");

• GridView_course.DataSource = ds_Row;• GridView_course.DataBind();• }

RowUpdating Event Execution Code

• void GridView_course_RowUpdating(object sender, GridViewUpdateEventArgs e)

• {• TextBox T_Subject =

(TextBox)GridView_course.Rows[e.RowIndex].Cells[3].Controls[0];

• TextBox T_CourseOff = (TextBox)GridView_course.Rows[e.RowIndex].Cells[4].Controls[0];

• dcon.inupdl("Update Course set courseOffer='" + T_CourseOff.Text + "' Where Subject='" + T_Subject.Text + "'");

• DataSet ds_Row = new DataSet();• ds_Row = dcon.Data_inventer("Select

Subject as Subject,courseOffer as Courses_Offer from Course order by Subject");

• GridView_course.DataSource = ds_Row;

• GridView_course.EditIndex = -1;• GridView_course.DataBind();• }

“FirstSample” is the key name in web config. Now Connection open & close• public SqlConnection open()• {• con_course.Open();• return con_course;• }• public SqlConnection close()• {• con_course.Close();• return null;• }

Simple code analysis

Creating a connection class: Using system.Data.Sqlclient; SqlConnection con_course;• //Connection Establishment With siliguri DataBase• public SqlConnection Siliguri_conn()• {• con_course = new

SqlConnection(ConfigurationManager.AppSettings["FirstSample"]);

• return con_course;• }

Insert Update Delete Command for DataBase

• public void inupdl(string str)• {• SqlCommand comm = new SqlCommand();• comm.Connection = Siliguri_conn();• comm.CommandText = str;• open();• comm.ExecuteNonQuery();• close();• }

Fill Datatable into DataSet From DataBase

• public DataSet Data_inventer(string str) { SqlDataAdapter adpt_inven = new

SqlDataAdapter(str, Siliguri_conn());• DataSet ds = new DataSet();• adpt_inven.Fill(ds);• return ds;• }

Execute statement

Default.aspx.cs page: Create a object for connec class connec com_info = new connec();Now run insert,Update & Delete command com_info.inupdl("insert into TestValue values('" +

TextBox1.Text + "','" + TextBox2.Text + "')");Now retrieve data from Table: DataSet ds=new DataSet(); ds. Data_inventer(“Select * From TestValue);

Binding Data into Table

GridView1.DataSource = ds; GridView1.DataBind();

Summery

• GridView Controls.• Several Operation into Gridview Control.• Connection with SQL.Server program and

execute SQL command.