.NET Unit - 5
.NET Unit - 5
NET
ADO.NET provides a bridge between the front end controls and the back end database.
The ADO.NET objects encapsulate all the data access operations and the controls
interact with these objects to display data, thus hiding the details of movement of data.
The following figure shows the ADO.NET objects at a glance:
Properties Description
CaseSensitive Indicates whether string comparisons within the data tables are
case-sensitive.
Events Gets the list of event handlers that are attached to this
component.
Prefix Gets or sets an XML prefix that aliases the namespace of the
DataSet.
The following table shows some important methods of the DataSet class:
Methods Description
Properties Description
PrimaryKey Gets or sets an array of columns as the primary key for the
table.
The following table shows some important methods of the DataTable class:
Methods Description
AcceptChanges Commits all changes since the last AcceptChanges.
GetChanges Returns a copy of the DataTable with all changes made since
the AcceptChanges method was called.
LoadDataRow Finds and updates a specific row, or creates a new one, if not
found any.
RejectChanges Rolls back all changes made since the last call to
AcceptChanges.
Properties Description
The following table shows some important methods of the DataRow class:
Methods Description
AcceptChanges Accepts all changes made since this method was called.
RejectChanges Rolls back all changes made since the last call to
AcceptChanges.
Example
So far, we have used tables and databases already existing in our computer. In this
example, we will create a table, add column, rows and data into it and display the table
using a GridView object.
The source file code is as given:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="createdatabase._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
<title>
Untitled Page
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
The code behind file is as given:
namespace createdatabase
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = CreateDataSet();
GridView1.DataSource = ds.Tables["Student"];
GridView1.DataBind();
}
}
// adding columns
AddNewColumn(Students, "System.Int32", "StudentID");
AddNewColumn(Students, "System.String", "StudentName");
AddNewColumn(Students, "System.String", "StudentCity");
// adding rows
AddNewRow(Students, 1, "M H Kabir", "Kolkata");
AddNewRow(Students, 1, "Shreya Sharma", "Delhi");
AddNewRow(Students, 1, "Rini Mukherjee", "Hyderabad");
AddNewRow(Students, 1, "Sunil Dubey", "Bikaner");
AddNewRow(Students, 1, "Rajat Mishra", "Patna");
return Students;
}
SP.NET has two controls that allow users to upload files to the web server.
Once the server receives the posted file data, the application can save it, check it, or
ignore it. The following controls allow the file uploading:
HtmlInputFile - an HTML server control
FileUpload - and ASP.NET web control
The FileUpload class is derived from the WebControl class, and inherits all its members.
Apart from those, the FileUpload class has the following read-only properties:
Properties Description
Properties Description
<div>
<h3> File Upload:</h3>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<br /><br />
<asp:Button ID="btnsave" runat="server" onclick="btnsave_Click" Text="Save"
style="width:85px" />
<br /><br />
<asp:Label ID="lblmessage" runat="server" />
</div>
</form>
</body>
The code behind the save button is as given:
protected void btnsave_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
if (FileUpload1.HasFile)
{
try
{
sb.AppendFormat(" Uploading file: {0}", FileUpload1.FileName);
DataBoundControl
HierarchicalDataBoundControl
The abstract class DataBoundControl is again inherited by two more abstract classes:
ListControl
CompositeDataBoundControl
The controls capable of simple data binding are derived from the ListControl abstract
class and these controls are:
BulletedList
CheckBoxList
DropDownList
ListBox
RadioButtonList
The controls capable of declarative data binding (a more complex data binding) are
derived from the abstract class CompositeDataBoundControl. These controls are:
DetailsView
FormView
GridView
RecordList
When the application is executed, check that the entire title column is bound to the
bulleted list and displayed.
Example
Let us take the following steps:
Step (1) : Create a new website. Add a class named booklist by right clicking on the
solution name in the Solution Explorer and choosing the item 'Class' from the 'Add Item'
dialog box. Name it as booklist.cs.
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace databinding
{
public class booklist
{
protected String bookname;
protected String authorname;
public booklist(String bname, String aname)
{
this.bookname = bname;
this.authorname = aname;
ASP.NET DataList
The ASP.NET DataList control is a light weight server side control that works as a
container for data items. It is used to display data into a list format to the web pages.
It displays data from the data source. The data source can be either a DataTable or a
table from database.
Here, first, we are creating DataList that gets data from a DataTable. This example
includes the following files.
22. </td>
23. </tr>
24. </table>
25. </ItemTemplate>
26. </asp:DataList>
27. </form>
28. </body>
29. </html>
CodeBehind
// DataListExample2.aspx.cs
1. using System;
2. using System.Collections.Generic;
3. using System.Data;
4. using System.Linq;
5. using System.Web;
6. using System.Web.UI;
7. using System.Web.UI.WebControls;
8. namespace DataListExample
9. {
10. public partial class DataListExample2 : System.Web.UI.Page
11. {
12. protected void Page_Load(object sender, EventArgs e)
13. {
14. DataTable table = new DataTable();
15. table.Columns.Add("ID");
16. table.Columns.Add("Name");
17. table.Columns.Add("Email");
18. table.Rows.Add("101", "Sachin Kumar", "[email protected]");
19. table.Rows.Add("102", "Peter", "[email protected]");
20. table.Rows.Add("103", "Ravi Kumar", "[email protected]");
21. table.Rows.Add("104", "Irfan", "[email protected]");
22. DataList1.DataSource = table;
23. DataList1.DataBind();
24. }
25. }
26. }
Output:
It is a server side control and can be dragged from the toolbox to the web form. Data
Source for the DataGrid can be either a DataTable or a database. Let's see an example,
how can we create a DataGrid in our application.
This tutorial contains two examples. One is using the DataTable and second is using the
database to display data into the DataGrid.
// DataGridExample2.aspx
CodeBehind
// DataGridExample2.aspx.cs
1. using System;
2. using System.Data;
3. namespace DataGridExample
4. {
5. public partial class DataGridExample2 : System.Web.UI.Page
6. {
7. protected void Page_Load(object sender, EventArgs e)
8. {
9. DataTable table = new DataTable();
10. table.Columns.Add("ID");
11. table.Columns.Add("Name");
12. table.Columns.Add("Email");
13. table.Rows.Add("101", "Deepak Kumar", "[email protected]");
14. table.Rows.Add("102", "John", "[email protected]");
15. table.Rows.Add("103", "Subramanium Swami", "[email protected]");
16. table.Rows.Add("104", "Abdul Khan", "[email protected]");
17. DataGrid1.DataSource = table;
18. DataGrid1.DataBind();
19. }
20. }
21. }
Output: