Top Banner
ASP.NET & ADO.NET Examples Hans-Petter Halvorsen Getting Started with ASP.NET and Database Communication using ADO.NET
44

ASP.NET and ADO.NET Example

Jan 12, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ASP.NET and ADO.NET Example

ASP.NET&ADO.NETExamples

Hans-PetterHalvorsen

GettingStartedwithASP.NETandDatabaseCommunicationusingADO.NET

Page 2: ASP.NET and ADO.NET Example

WebFormandADO.NETExamples

WebFormExample1WebFormExample2

InthisTutorialwewillcreatethefollowingExamples:

Page 3: ASP.NET and ADO.NET Example

WebFormandADO.NETExamples

WebFormExample3

InthisTutorialwewillcreatethefollowingExamples:

WebFormExample4

Page 4: ASP.NET and ADO.NET Example

SQLServerDatabase

Hans-PetterHalvorsen

Page 5: ASP.NET and ADO.NET Example

CreateDatabaseandTableusedinExamples

Page 6: ASP.NET and ADO.NET Example

WritingDatatoDatabase

Hans-PetterHalvorsen

WebFormExample1

Page 7: ASP.NET and ADO.NET Example

WebFormExample1WewillcreatethefollowingWebFormExample:

Page 8: ASP.NET and ADO.NET Example

CreateASP.NETProject

Page 9: ASP.NET and ADO.NET Example

CreateW

ebForm

(Web

Form

1.aspx)

DragTextboxesandaButtonfromtheToolboxandupdateProperties

FinetuneGUIelementsintheCodewindow

Double-clicktocreateEventHandler

Page 10: ASP.NET and ADO.NET Example

ASPXCode(WebForm1.aspx)<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Database_Web_Example.WebForm1" %>

<!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>

Registration Number:<br /><asp:TextBox ID="txtRegistrationNumber" runat="server" Width="200px"></asp:TextBox><br />

Car Type:<br /><asp:TextBox ID="txtCarType" runat="server" Width="200px"></asp:TextBox><br /><br />

<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save Data" />

</div></form>

</body></html>

Page 11: ASP.NET and ADO.NET Example

CreateC#Code(WebForm1.aspx.cs)

YourDatabase

Page 12: ASP.NET and ADO.NET Example

CreateC#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){

}

protected void btnSave_Click(object sender, EventArgs e){

string sqlQuery = "INSERT INTO CAR VALUES (" + "'" + txtRegistrationNumber.Text + "'" + "," + "'" + txtCarType.Text + "'" + ")";

SqlConnection conDB = new SqlConnection("Data Source=<YourDatabase>;Initial Catalog=TEST;Integrated Security=True");

conDB.Open();

SqlCommand sc = new SqlCommand(sqlQuery, conDB);

sc.ExecuteNonQuery();

conDB.Close();

}}

}

Page 13: ASP.NET and ADO.NET Example

VisualStudioProject

Page 14: ASP.NET and ADO.NET Example

RunningWebForm

Page 15: ASP.NET and ADO.NET Example

EnteringandSavingData

Page 16: ASP.NET and ADO.NET Example

YouarefinishedwiththeExample

Page 17: ASP.NET and ADO.NET Example

GettingDatafromDatabase

Hans-PetterHalvorsen

WebFormExample2

Page 18: ASP.NET and ADO.NET Example

WebFormExample2WewillcreatethefollowingWebFormExample:

Page 19: ASP.NET and ADO.NET Example

Createanew

Web

Form

(Web

Form

2.aspx)

Page 20: ASP.NET and ADO.NET Example

ASPXCode(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>

Car Type List:<br /><asp:DropDownList ID="listCarTypes" runat="server" Height="16px" Width="200px"></asp:DropDownList><br />

</div></form>

</body></html>

Page 21: ASP.NET and ADO.NET Example

CreateC#Code(WebForm2.aspx.cs)

YourDatabase

Page 22: ASP.NET and ADO.NET Example

CreateC#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){

SqlConnection conDB = new SqlConnection("Data Source=<YourDatabase>;Initial Catalog=TEST;Integrated Security=True");

conDB.Open();

string sqlQuery = "SELECT CarType FROM CAR ORDER BY CarType";

SqlCommand sc = new SqlCommand(sqlQuery, conDB);

SqlDataReader reader;

reader = sc.ExecuteReader();

while (reader.Read()){

ListItem newItem = new ListItem();

newItem.Text = reader["CarType"].ToString();listCarTypes.Items.Add(newItem);

}

conDB.Close();}

}}

Page 23: ASP.NET and ADO.NET Example

VisualStudioProject

Page 24: ASP.NET and ADO.NET Example

RunningWebForm

Page 25: ASP.NET and ADO.NET Example

YouarefinishedwiththeExample

Page 26: ASP.NET and ADO.NET Example

GettingspesificDatafromDatabasebasedonUserInteraction

Hans-PetterHalvorsen

WebFormExample3

Page 27: ASP.NET and ADO.NET Example

WebFormExample3WewillcreatethefollowingWebFormExample:

Page 28: ASP.NET and ADO.NET Example

Createanew

Web

Form

(Web

Form

3.aspx)

Page 29: ASP.NET and ADO.NET Example

ASPXCode(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>

Car Number List:<br /><asp:DropDownList ID="listCarNumbers" runat="server" Height="16px" Width="200px"></asp:DropDownList><br /><br />

<asp:Button ID="btnGetCarType" runat="server" OnClick="btnGetCarType_Click" Text="Get CarType" /><br />The Car Type is: <asp:Label ID="lblCarNumber" runat="server" Text="Label"></asp:Label><br />

</div></form>

</body></html>

Page 30: ASP.NET and ADO.NET Example

CreateC#Code(WebForm3.aspx.cs)

Page 31: ASP.NET and ADO.NET Example

CreateC#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();}

}

protected void btnGetCarType_Click(object sender, EventArgs e){

GetCarType();}

}}

Page 32: ASP.NET and ADO.NET Example

CreateC#Code(WebForm3.aspx.cs)voidFillCarNumberList()

{

SqlConnection conDB =newSqlConnection("DataSource=<YourDatabase>;;InitialCatalog=TEST;Integrated Security=True");

conDB.Open();

stringsqlQuery ="SELECTRegistrationNumber FROMCARORDERBYRegistrationNumber";

SqlCommand sc =newSqlCommand(sqlQuery,conDB);

SqlDataReader reader;

reader=sc.ExecuteReader();

while(reader.Read()){ListItem newItem =newListItem();

newItem.Text =reader["RegistrationNumber"].ToString();listCarNumbers.Items.Add(newItem);

}conDB.Close()

}

Page 33: ASP.NET and ADO.NET Example

CreateC#Code(WebForm3.aspx.cs)void GetCarType()

{SqlConnection conDB = new SqlConnection("Data Source=<YourDatabase>;Initial Catalog=TEST;Integrated Security=True");

conDB.Open();

string sqlQuery = "SELECT CarType FROM CAR WHERE RegistrationNumber='" + listCarNumbers.SelectedItem.Text + "'";

SqlCommand sc = new SqlCommand(sqlQuery, conDB);

SqlDataReader reader;

reader = sc.ExecuteReader();

if (reader.Read()){

lblCarNumber.Text = reader["CarType"].ToString(); }

conDB.Close();

}

}}

Page 34: ASP.NET and ADO.NET Example

VisualStudioProject

Page 35: ASP.NET and ADO.NET Example

RunningWebForm

Page 36: ASP.NET and ADO.NET Example

YouarefinishedwiththeExample

Page 37: ASP.NET and ADO.NET Example

Layout

Hans-PetterHalvorsen

Page 38: ASP.NET and ADO.NET Example

Bootstrap

Hans-PetterHalvorsen

WebFormExample4

Page 39: ASP.NET and ADO.NET Example

Bootstrap• BootstrapisthemostpopularHTML,CSS,andJavaScript

frameworkfordevelopingresponsive,mobile-firstwebsites.• Bootstrapiscompletelyfreetodownloadanduse!• Bootstrapisafreefront-endframeworkforfasterandeasier

webdevelopment• BootstrapincludesHTMLandCSSbaseddesigntemplatesfor

typography,forms,buttons,tables,navigation,modals,etc.• Bootstrapalsogivesyoutheabilitytoeasilycreateresponsive

designshttp://getbootstrap.comhttp://www.w3schools.com/bootstrap/

Page 40: ASP.NET and ADO.NET Example

CreateLayoutwithBootstrap

Page 41: ASP.NET and ADO.NET Example

<%@ 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 panel-success">

<div class="panel-heading">

<h1>Car Example</h1>

</div>

<div class="panel-body">

<label for="txtRegistrationNumber">Registration Number:</label><asp:TextBox ID="txtRegistrationNumber" runat="server" Width="200px" class="form-control"></asp:TextBox>

<label for="txtCarType">Car Type:</label><asp:TextBox ID="txtCarType" runat="server" Width="200px" class="form-control"></asp:TextBox>

<br />

<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save Data" class="btn btn-success" />

</div>

</div>

</div></form>

</body></html>

Page 42: ASP.NET and ADO.NET Example

FinalResults

Page 43: ASP.NET and ADO.NET Example

YouarefinishedwiththeExample

Page 44: ASP.NET and ADO.NET Example

Hans-PetterHalvorsen,M.Sc.

UniversityCollegeofSoutheastNorwaywww.usn.no

E-mail:[email protected]:http://home.hit.no/~hansha/