And ADO - NET Example PDF
And ADO - NET Example PDF
And ADO - NET Example PDF
Hans-Petter Halvorsen
Web Form and ADO.NET Examples
In this Tutorial we will create the following Examples:
Hans-Petter Halvorsen
Create Database and Table used in
Examples
Web Form Example 1
Hans-Petter Halvorsen
Web Form Example 1
We will create the following Web Form Example:
Create ASP.NET Project
Finetune GUI elements in
(WebForm1.aspx)
the Code window
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Car Registration Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Car Example</h1>
</div>
</form>
</body>
</html>
Create C# Code (WebForm1.aspx.cs)
Your Database
Create C# Code (WebForm1.aspx.cs)
using System;
using System.Data.SqlClient;
namespace Database_Web_Example
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sqlQuery = "INSERT INTO CAR VALUES (" + "'" + txtRegistrationNumber.Text + "'" + "," + "'" + txtCarType.Text + "'" + ")";
conDB.Open();
sc.ExecuteNonQuery();
conDB.Close();
}
}
}
Visual Studio Project
Running Web Form
Entering and Saving Data
You are finished with the Example
Web Form Example 2
Hans-Petter Halvorsen
Web Form Example 2
We will create the following Web Form Example:
Create a new
Web Form
(WebForm2.aspx)
ASPX Code (WebForm2.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="Database_Web_Example.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Car List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Car Example</h1>
</div>
</form>
</body>
</html>
Create C# Code (WebForm2.aspx.cs)
Your Database
Create C# Code (WebForm2.aspx.cs)
using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace Database_Web_Example
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
conDB.Open();
SqlDataReader reader;
reader = sc.ExecuteReader();
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["CarType"].ToString();
listCarTypes.Items.Add(newItem);
}
conDB.Close();
}
}
}
Visual Studio Project
Running Web Form
You are finished with the Example
Web Form Example 3
Hans-Petter Halvorsen
Web Form Example 3
We will create the following Web Form Example:
Create a new
Web Form
(WebForm3.aspx)
ASPX Code (WebForm3.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs"
Inherits="Database_Web_Example.WebForm3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Select Car Registration Number</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Car Example</h1>
</div>
</form>
</body>
</html>
Create C# Code (WebForm3.aspx.cs)
Create C# Code (WebForm3.aspx.cs)
using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace Database_Web_Example
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillCarNumberList();
}
}
}
}
Create C# Code (WebForm3.aspx.cs)
void FillCarNumberList()
{
conDB.Open();
SqlDataReader reader;
reader = sc.ExecuteReader();
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["RegistrationNumber"].ToString();
listCarNumbers.Items.Add(newItem);
}
conDB.Close()
}
Create C# Code (WebForm3.aspx.cs)
void GetCarType()
{
SqlConnection conDB = new SqlConnection("Data Source=<Your Database>;Initial Catalog=TEST;Integrated Security=True");
conDB.Open();
string sqlQuery = "SELECT CarType FROM CAR WHERE RegistrationNumber='" + listCarNumbers.SelectedItem.Text + "'";
SqlDataReader reader;
reader = sc.ExecuteReader();
if (reader.Read())
{
lblCarNumber.Text = reader["CarType"].ToString();
}
conDB.Close();
}
}
Visual Studio Project
Running Web Form
You are finished with the Example
Layout
Hans-Petter Halvorsen
Web Form Example 4
Bootstrap
Hans-Petter Halvorsen
Bootstrap
• Bootstrap is the most popular HTML, CSS, and JavaScript
framework for developing responsive, mobile-first web sites.
• Bootstrap is completely free to download and use!
• Bootstrap is a free front-end framework for faster and easier
web development
• Bootstrap includes HTML and CSS based design templates for
typography, forms, buttons, tables, navigation, modals, etc.
• Bootstrap also gives you the ability to easily create responsive
designs
http://getbootstrap.com
http://www.w3schools.com/bootstrap/
Create Layout with Bootstrap
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="Database_Web_Example.WebForm4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Car Registration Form</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div class="container-fluid">
<div class="panel-heading">
<h1>Car Example</h1>
</div>
<div class="panel-body">
<br />
</div>
</div>
</div>
</form>
</body>
</html>
Final Results
You are finished with the Example
Hans-Petter Halvorsen, M.Sc.
E-mail: [email protected]
Blog: http://home.hit.no/~hansha/