20
CST141ASP.NET Database Page 1 ASP.NET Database CST242 Database A database (the data) – A computer filing system • I.e. Payroll, personnel, order entry, billing, accounts receivable and payable, contact list, etc. A database management system (DBMS) (the software) – Program that manipulates the database – A DBMS may have a GUI front end, but its database engine physically processes the data Database Processing Maintenance – Keeping the database up-to-date (current) – Adding, modifying and deleting records Retrieval—allows access of meaningful information – On-line (on the monitor/screen) – Printed reports Database Terminology Database – A collection of data tables and relationships Table – Individual categories of data entities Record – All of the data for one person or entity – Called a row in relational DB terminology Field – One element of data within a record – Called a column in relational DB terminology The Primary Key Field or concatenation (combination) of fields which uniquely identify records in the table … – Most efficient tool for searching for specific records The primary key also is an index which controls the default sort order for the table 1 2 3 4 5 2 3 4 5 Structured Query Language SQL (pronounced see’-quel) – A relational database language developed by IBM in mid-1970's Represents any data as one or more tables Non-procedural language ... 8 8

CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

  • Upload
    others

  • View
    25

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 1

ASP.NET Database

CST242

Database

• A database (the data)

– A computer filing system

• I.e. Payroll, personnel, order entry, billing, accounts receivable and payable,

contact list, etc.

• A database management system (DBMS) (the software)

– Program that manipulates the database

– A DBMS may have a GUI front end, but its database engine physically processes the

data

Database Processing

• Maintenance

– Keeping the database up-to-date (current)

– Adding, modifying and deleting records

• Retrieval—allows access of meaningful information

– On-line (on the monitor/screen)

– Printed reports

Database Terminology

• Database

– A collection of data tables and relationships

• Table

– Individual categories of data entities

• Record

– All of the data for one person or entity

– Called a row in relational DB terminology

• Field

– One element of data within a record

– Called a column in relational DB terminology

The Primary Key

• Field or concatenation (combination) of fields which uniquely identify records in the

table …

– Most efficient tool for searching for specific records

• The primary key also is an index which controls the default sort order for the table

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

2

3

4

5

Structured Query Language

• SQL (pronounced see’-quel)

– A relational database language developed by IBM in mid-1970's

• Represents any data as one or more tables

• Non-procedural language ...

– Lets the DBMS engine determine how the operation will be executed

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

7

8

Page 2: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 2

• Non-procedural language ...

– Lets the DBMS engine determine how the operation will be executed

• SQL commands can be embedded into procedural language programs like Java

Data Retrieval in SQL

• Reporting in both printed and on-line formats

• Basic format:

SELECT columnList

FROM table(s)

WHERE condition

– SELECT clause limits columns returned (required)

– FROM clause names table from which data is extracted (required)

– WHERE clause limits rows returned (optional)

Basic SELECT statement

• Format:

SELECT *

FROM tableName

• The “*” is a wildcard that specifies all columns

• All rows also will be returned since there is no WHERE clause

• Example:

SELECT * FROM Book

SELECT with column list

• A comma-delimited list of column names (fieldnames) that limits which columns are

returned in a query

• Format:

SELECT columnName1[, columnName2 … ]

FROM tableName

• Example:

SELECT BookCode, Title, Price

FROM Book

The WHERE Clause (Page 1)

• Based on the truth condition of a relation condition (like in an if statement) that limits

rows returned

• At least one factor in the WHERE condition will be a column name

• Format:

SELECT columnName1[, columnName2 … ]

FROM tableName(s)

WHERE relation_condition

The WHERE Clause (Page 2)

• Examples:

SELECT Title, Type, Price

FROM Book

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

9

10

11

12

13

Page 3: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 3

SELECT Title, Type, Price

FROM Book

WHERE Type = "MYS"

SELECT Title, PublisherCode

FROM Book

WHERE Not Paperback

The ORDER BY Clause (Page 1)

• Sorts the rows in the returned table sorting on the column or columns named in the

ORDER BY clause

• Format:

SELECT columnName1[, columnName2 … ]

FROM tableName(s)

ORDER BY columnName1[, columnName2 …]

The ORDER BY Clause (Page 2)

• Example:

SELECT BookCode, Title, Price

FROM Book

ORDER BY Title

• To sort on major and minor sort fields:

SELECT PublisherCode, Title

FROM Book

ORDER BY PublisherCode, Title

Relationships (Linking)

• In databases with more than one table, there is a link on a common field

• This link (and the relational rules that apply to it) must be enforced

– By the DBMS as well as by Try…Catch processing in the application

• Referential integrity is the rule which states that a common field in one table must

match primary key in the other, or be null (blank)

The Foreign Key

• The field that links a table in a relational database to the primary key in another table

• There may be multiple instances of a single value of the foreign key; however …

• The foreign key must match one of the values of the primary key in the table to which

it links, or by null (blank)

SELECT with Join (Page 1)

• A join operation links related fields from more than one table

• Format:

SELECT columnName1[, columnName2 … ]

FROM tableName1

INNER JOIN tableName2 ON primaryKey = foreignKey

– The datatype and field size of the primaryKey and foreignKey fields must be the

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

14

15

16

17

18

Page 4: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 4

INNER JOIN tableName2 ON primaryKey = foreignKey

– The datatype and field size of the primaryKey and foreignKey fields must be the

same

SELECT with Join (Page 2)

• Example:

SELECT BookCode, Title, Name

FROM Book

INNER JOIN Publisher

ON Book.PublisherCode = Publisher.PublisherCode

– Column names that exist in more than one table must be prefixed by the table

name, i.e. Book.PublisherCode and Publisher.PublisherCode

SELECT with Join and ORDER BY and/or WHERE

• WHERE and ORDER BY clauses may be combined into the same SQL statement

• Example:

SELECT BookCode, Title, Name

FROM Book

INNER JOIN Publisher

ON Book.PublisherCode = Publisher.PublisherCode

ORDER BY Name, Title

WHERE Type = "MYS"

An ASP.NET Web Site Project

• In Visual Studio a Web Site is a “repository” (folder/storage location) that contains files

and additional folders that make up the project

Creating a New Web Site Project (Page 1)

• To create a new ASP.NET project:

1. Select the File command from the menu bar

2. Click New from the “File” menu

3. Click Project… from the “New” submenu; or …

Creating a New Web Site Project (Page 2)

• Alternately to create the new project:

1. Select the down arrow point () on the New Project button from the “Standard”

toolbar

2. Click New Project… from the “New Project” submenu

Creating a New Web Site Project (Page 3)

• Then in the “New Project” dialog window:

1. Make sure to verify that Visual C# is selected in the “Installed Templates” pane

2. Select ASP.NET Web Application (or ASP.NET Empty Web Application—our

preference in CST242) template

3. Give the Web Site a Name:

4. Make sure Create directory for solution checkbox stays checked

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

19

20

21

22

23

24

Page 5: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 5

Creating a New Web Site Project (Page 4)

• In the “New Web Site” dialog window (con.):

4. Enter the project folder Location and name

• Click the <Browse…> button to make it easier to select the folder location

5. Click the <OK> button

• In the “New ASP.NET Project” dialog window, from “Select a template: select option

Empty and click the <OK> button (make no other changes)

• In the “Configure Microsoft Azure Web App” dialog window click the <Cancel> button

An ASP.NET Web Site (Page 1)

• Types of files in a website:

– ASP.NET web forms—dynamic (interactive) web forms with two files: (1) the .aspx

file which is the web page; and (2) the .aspx.cs file that contains the Visual C# code

that runs on the server

– Image files—for the website

– Configuration files—web.config is a file that provides information for the server

– Static HTML web pages—HTML code only; no ASP .NET Server Web controls

An ASP.NET Web Site (Page 2)

• Types of files in a web site (con.):

– Style sheet files—additional information that tells the browser how to format HTML

elements; useful for creating consistent style on a Web page, or all the Web pages

on a website

– Script files—source code that runs on the client machine in the browser, e.g.

JavaScript or VbScript

– Database files

Web Forms in ASP.NET (Page 1)

• The Web Form consists of two components:

– The HTML template (“.aspx” extension)

• The actual web page that contains the design layout, content and controls

– A collection of code in a procedure language such as C# that commonly is located

behind the Web Form (“aspx.cs” extension)

• The “code behind the page”

Web Forms in ASP.NET (Page 2)

• The ASP.NET Form Web control handles most of the HTML detail processing for the

developer

• The input controls are generated using ASP.NET web controls, i.e. ASP:TextBox and

ASP:Label

• The ASP:Button Web control is rendered by ASP.NET as a submit button

– The form is submitted when the button is clicked

Web Forms in ASP.NET (Page 3)

• The format of the Form web control which wraps around the input elements is:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

25

26

27

28

29

30

31

Page 6: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 6

Web Forms in ASP.NET (Page 3)

• The format of the Form web control which wraps around the input elements is:

<form id="FormID" runat="server">

</form>

– This block is inserted automatically into every new ASP.NET web document

Web Forms in ASP.NET (Page 4)

• When the Button control is clicked, the Web form is rendered as a postback <form>

element (a POST method) which causes the web form to be reloaded …

– Code (Visual C#) that should execute when the form is submitted is assigned to the

Click event of the Button

• Additional hidden <input> elements are generated which provide information to

ASP.NET

Adding an Existing Item

• Existing items might include ASP.NET Web Forms, HTML pages, databases, image

files, etc.

• Start by being certain folder that item is to be inserted into is selected in “Solution

Explorer”

1. Select Project from the menu bar

2. Select Add Existing Item… from the Website menu

3. Browse to Look in: folder

4. Select Files of type: from drop-down list

5. Select files and click <Add> button; the item(s) is/are added to specified folder in

Solution Explorer

Adding a New Item (Page 1)

• To add a new item/document to an ASP.NET project:

1. Select Project from the menu bar

2. Click Add New Item… from the “Website” menu

• Or find “Add New Item” by right-clicking the website folder name in the “Solution

Explorer” window

Adding a New Item (Page 2)

• In the “Add New Item” dialog window:

1. Make sure to verify that Web is selected under Visual C# in the “Installed” pane

2. Select the document type—an ASP.NET document page is a Web Form

3. Type filename (appropriate extension will be added)

4. Click <Add> button—the new item is added to the “Solution Explorer” window

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

31

32

33

34

35

The Solution Explorer Window

• Window on the upper right of IDE that lists the project files and resources (images,

databases, etc.)

Save the Web Form

• To save current document, click the <Save> button on the “Standard” toolbar

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

36

37

38

Page 7: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 7

Save the Web Form

• To save current document, click the <Save> button on the “Standard” toolbar

• It should not be necessary to name it in the “Save As” dialog window since it was given

a name when it initially was created

Open a Web Site (Page 1)

• To open an existing ASP.NET project:

1. Select the File command from the menu bar

2. Click Open from the “File” menu

3. Click Project… from the “Open” submenu

Open a Web Site (Page 2)

• In the “Open Web Site” dialog window:

1. Select the folder name that contains the Solution file (the main “top” folder)

2. Select the filename with the .sln extension

3. Click the <Open> button

HTML (and ASP.NET) Tags

• Formatting codes that instruct the browser how to display page elements

• Tags are enclosed in angle brackets (<…>) …

• Most tags are two-sided …

– First tag tells browser to turn on feature

– Second tag instructs browser to turn it off

– E.g. <b> begins boldface, </b> turns it off

Sections of a Web Document

• Web pages in HTML usually are divided into two main sections …

– head: defines the title of the page, information about the page to help search

engines find it, style sheets, etc.

– body: specifies the content (visual elements) of the Web page

HTML Basic Page Outline

<html>

<head>

identifying elements

</head>

<body>

visual content elements

</body>

</html>

<html>

• Encloses the entire HTML file

• Identifies the file to browser software as one containing HTML code, e.g.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

38

39

40

41

42

43

44

Page 8: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 8

• Encloses the entire HTML file

• Identifies the file to browser software as one containing HTML code, e.g.

– Microsoft Internet Explorer

– Foxfire

• Closing tag is </html>

<head>

• Encloses the heading elements of an HTML file, e.g.

– <title> (the title bar element)

– <bgsound> (background sound file)

– <style> (links to style sheet files)

– <meta> (provides links for search engines)

• Except for <title> the other elements are not visible to the viewer

• Closing tag is </head>

<title>

• Defines text displayed in tabs in the browser when the page is displayed

• Appears inside the <head> … </head> block

• The <title> tag is a required element

• A title directly reflects a page’s ranking in most search engines

• Closing tag is </title>

<body>

• Encloses body elements of HTML file …

– Elements that appear on the Web page

• Format:

<body>

• Closing tag is </body>

The <img> (Image) Tag (Page 1)

• The src attribute must be used to name an image (graphic) file to be displayed on

page

• Path is required if file is not located in same directory as the HTML document

• Format:

<img src="path/filename" />

• Example:

<img src="images/sccclogob.jpg" />

The <img> (Image) Tag (Page 2)

• There is no closing tag for <img> so it should included a slash (/) meaning the “end”

symbol inside the tag itself, e.g.

<img />

Dragging an Image from Solution Explorer

• In either Source or Design view, the designer and drag an image from the Solution

Explorer window into the document

• Creates an HTML <img> tag in the document

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

45

46

47

48

49

50

Page 9: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 9

Explorer window into the document

• Creates an HTML <img> tag in the document

Properties Window

• Window on the lower right of IDE where designer sets the properties (characteristics)

for objects, controls, classes and other project components

• Properties can be updated:

– At design time by changing their values in the Properties Window

– By writing Visual C# assignment statements in the program code behind the page,

e.g.

TextBoxCity.Text = "New York";

Using the Style Attribute for Formatting

• CSS syntax and the style attribute can be used for the formatting of HTML elements

• To create a style attribute for absolute positioning of an element, e.g.

<div style="position:absolute; top:200; left:200" />

Document Views

• The tabs at the bottom left of the “Document” window lets the designer select among

the following views:

– Source view—the actual ASP.NET code

– Design view—an approximation of the Web page that the code will render in a

browser

– Split view—Source view in the upper window and Design view in the lower window

“Run” the Web Site

• “Running” an ASP.NET Web Site application means to view it in a Web browser

• An application may be executed for testing by clicking the <Start debugging> button

on the “Standard” toolbar

• Close the browser window to stop “running”

– It may be necessary to click the <Stop debugging> button () on the toolbar after

browser window closes

ASP.NET Server Controls

• Controls are similar to Windows Forms controls

• ASP.NET controls are identified with the prefix asp: followed by the name of the

control, plus a runat="server" attribute and value, e.g.

<asp:TextBox runat="server">

• Some types of ASP.NET Server Controls

– ASP.NET Form Controls (Web controls)

– Data Validation Controls

– Data Controls

– Mobile Controls (run on mobile devices)

The Toolbox

• Provides access to commonly used controls, e.g. the ASP.NET Server controls

• Can be hidden and made to slide out—the Auto Hide feature

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

51

52

53

54

55

56

Page 10: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 10

• Provides access to commonly used controls, e.g. the ASP.NET Server controls

• Can be hidden and made to slide out—the Auto Hide feature

– Hover over the text “Toolbox” in the left side of the IDE window and it slides into

view (the default mode for the Toolbox is hidden)

– Click the Auto Hide icon (push pin) to turn the feature on and off

The ASP:SqlDataSource Web Control (Page 1)

• The asp:SqlDataSource Web control is used to retrieve and modify (insert, update, and

delete) data from a database with little or no code

• Can work with any database that has an associated ADO.NET provider

– E.g. Microsoft SQL Server, Oracle, ODBC, or OLE DB databases such as Microsoft

Access

The ASP:SqlDataSource Web Control (Page 2)

• The SqlDataSource requires that two properties be set (performed by clicking the

“smart tag” and selecting the command “Configure Data Source”):

– ConnectionString—specifies the database type, location and filename

• Created automatically in the “Web.config” file the first time the SqlDataSource is

linked to SqlServer database in the “Configure Data Source” wizard

– SelectCommand—set to an SQL SELECT query

• Created automatically by using the Configure the Select Statement window of the

wizard

Data Source Configuration: New Data Source

• Switch to Design View and click the “smart tag” to access the Configure Data Source

wizard:

– On the Configure Data Source window click the <New Connection…> button

– In the Add Connection window, select “Microsoft SQL Server Database File” as the

Data Source

– Click <Continue> and <Browse…> to find database

– Select it and click the <Open> button

– Choose Yes, save the connection as to store the connection string in the

“Web.config” file

– Click <Next> to go to next window of the wizard

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

57

58

59

Data Source Configuration: Existing Connection String

• Switch to Design View and click the “smart tag” to access the Configure Data Source

wizard:

– Select the Connection string associated with the desired database from the drop-

down list of existing connection strings

– Click <Next> to go to next window of the wizard

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

62

63

Build the SQL Select Statement (Page 1)

• The Configure the Select Statement step of wizard specifies how to create SELECT

statement:

– Specify columns from a table or view:

• Select the table name and check the columns (fields) of data to be included in

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

64

65

Page 11: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 11

– Specify columns from a table or view:

• Select the table name and check the columns (fields) of data to be included in

the SELECT query

• Further define the statement by using the <WHERE> and <ORDER BY> buttons

• The SELECT statement is visible under the heading SELECT statement:

• Click <Next> and then <Finish> buttons

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

Build the SQL Select Statement (Page 2)

• The Configure the Select Statement step of wizard specifies how to create SELECT

statement (con.):

– Specify a custom SQL statement or stored procedure:

• Click <Next> and then click the <QueryBuilder> button to access an intuitive user

interface for creating the SELECT statement

• Click <Add> to add tables to query (then <Close>)

• Check columns for the query; then click <OK>

Build the SQL Select Statement (Page 3)

• The Configure the Select Statement step of wizard specifies how to create SELECT

statement (con.):

– Specify a custom SQL statement or stored procedure (con.):

• Finally click <Next> and then <Finish> buttons

• This tool is helpful especially when joining data from multiple tables

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

66

67

68

The ASP:SqlDataSource Web Control for Inserting, Updating and Deleting

• Once the SELECT statement is specified …

– Click the <Advanced> button which displays the “Advanced SQL Generation

Options” dialog window

– Click “on” the checkbox for the Generate INSERT, UPDATE, and DELETE

statements checkbox

– Adds InsertCommand, UpdateCommand, and DeleteCommand properties to

<asp:SqlDataSource> control tag

The ASP:GridView Web Control (Page 1)

• Automatically binds to and displays data from a data source control in tabular view

(rows and columns)

• To assign the data source:

– Click the “smart tag” and select a data source (e.g. an SQLDataSource object) from

the Choose Data Source: drop-down list

• The source code is updated automatically to include a <Columns> block and

<asp:BoundField> tags which represent the data (usually)

• This formats the columns to represent the fields of the selected query (the data

source)

The ASP:GridView Web Control (Page 2)

• When a data source is selected for the GridView control, the following of its properties

are updated automatically:

– AutoGenerateColumns—how are fields generated?:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

70

71

72

73

Page 12: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 12

are updated automatically:

– AutoGenerateColumns—how are fields generated?:

• True—from the data source at run-time

• False—uses columns defined in <Columns> block

– DataKeyNames—the DataField property of the BoundColumn(s) that uniquely

identify each record (by default the primary key)

– DataSourceID—the ID of its data source controls

Customizing ASP:GridView Using AutoFormat

• Formats the GridView by allowing user to select from a series of predefined styles

• Click the “smart tag” for the GridView and click the AutoFormat… command

• Select one of the predefined styles to preview it

• Click the <OK> button to implement the style

– The appropriate properties are set in the source code

The SQL UPDATE Command (Page 1)

• Modifies values in an existing row (or rows) within a database table

• Not all fields need be updated

• Format:

UPDATE TableName

SET field1 = value1/"string1"[, field2 = value2/"string2", … ]

[WHERE fieldName = value/"string"]

– value(s) may be numeric, Boolean, Date, etc

The SQL UPDATE Command (Page 2)

• Examples:

UPDATE Book

SET Title = "Of Mice and Women",

PublisherCode = "BB",

Type = "FIC",

Price = 12.59,

Paperback = .F.

WHERE BookCode = "7711"

UPDATE Book

SET Price = 12.59

WHERE BookCode = "7711"

The SQL DELETE Command

• Deletes a row (or more than one row) from a database table

• Format:

DELETE FROM TableName

[WHERE fieldName = value/"string"]

• Example:

DELETE FROM Book

WHERE BookCode = "7711"

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

74

75

76

77

Page 13: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 13

DELETE FROM Book

WHERE BookCode = "7711"

SQL Parameters (Page 1)

• Visual Studio uses parameters to represent variable data in a “parameterized” SQL

statement

• It is an object that can accept different values based upon the logic of the application

– The values are determined dynamically at run-time

SQL Parameters (Page 2)

• When “Generate INSERT, UPDATE, AND DELETE statements” is selected, there are three

parameter blocks inserted for each into the <asp:SqlDataSource> tag:

– <InsertParameters>—parameters used within the SQL Insert statement

– <UpdateParameters>—parameters used within the SQL Update statement

– <DeleteParameters>—parameters used within the SQL Delete statement

SQL Parameters (Page 3)

• The properties for the <asp:Parameter> tag include:

– Name—used within the parameterized SQL statement

– Type—data type which is a Visual C# type consistent with the type in the database

– Format:

<asp:Parameter Name="nameProperty" Type="dataType" />

– Example:

<asp:Parameter Name="ProductID" Type="String" />

SQL Parameters (Page 4)

• A parameter is a variable in the form of a question mark (?) within a “parameterized

SQL statement”

• Example of a parameterized SQL statement:

<asp:SqlDataSource …

DeleteCommand="DELETE FROM [Product] WHERE [ProductID] = ?" … >

– The question mark (?) is the parameter

Editing Data with GridView (Page 1)

• In GridView records can be edited by adding an Edit command (button or link) within

the CommandField column for each record

• When the button/link is clicked:

– The page is posted back and all editable fields (not the primary key or other fields

set to be read-only) are converted to TextBoxes for updating

– During editing the Edit button is converted to two buttons, Update and Cancel

Editing Data with GridView (Page 2)

• When the Update button is clicked:

– The page is posted back again

– The data source’s UpdateCommand is executed

– The GridView retrieves that updated table from the data source and redisplays the

data

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

78

79

80

81

82

83

Page 14: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 14

– The GridView retrieves that updated table from the data source and redisplays the

data

• If the Cancel button is clicked:

– The page is posted back, but GridView is updated from the unmodified data source

• To configure GridView for editing, click smart tag and click “on” the Enable Editing

checkbox

Editing Data with GridView (Page 3)

• To configure GridView for editing records, click the “smart tag” and click to check “on”

the Enable Editing checkbox

– Adds a <asp:CommandField> to the <Columns> block with the ShowEditButton

property set to True

<asp:CommandField ShowEditButton="True" />

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

84

<h1> through <h6>

• In HTML the <h1> through <h6> tags indicate a heading line and its level …

– Largest font size is <h1>, smallest is <h6>

– Text displayed in bold font style

– Automatic double spacing also is implemented after the heading text

• Often used to indicate titles or to organize a page into sections

• Closing tags are </h1> through </h6>

<p>

• In HTML divides text into paragraphs

• Automatic double spacing also is implemented within this tag

• Closing tag is </p>

The ASP:TextBox Web Control (Page 1)

• The asp:TextBox collects text input from users

• When the TextMode property is set to SingleLine, (default if not entered) creates a

one-line textbox

• Format:

<asp:TextBox ID="id" runat="server"> </asp:TextBox>

The ASP:TextBox Web Control (Page 2)

• Properties:

– (ID): the object’s name

– Text: the text displayed in the text box (updated as a user keys new or updated text)

The ASP:Label Control (Page 1)

• A Label control is an “unattached” text object that has a series of properties used for

formatting

• The Text property stores the Label’s text that appears on the web form

The ASP:Label Control (Page 2)

• Format:

<asp:Label ID="LabelID" runat="server" Text="Label Text" [formatting

properties]></asp:Label>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

85

86

87

88

89

90

91

Page 15: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 15

<asp:Label ID="LabelID" runat="server" Text="Label Text" [formatting

properties]></asp:Label>

• Example:

<asp:Label ID="Label1" runat="server" Text="Publisher Code:"

Width="125"></asp:Label>

The Button Web Control (Page 1)

• It functions as an HTML submit button (the form is submitted to the server)

– A form is most often submitted by clicking on a button

• The <asp:Button> Web server control in ASP.NET has the following format:

– The control is used to display a push button

– The push button may be a submit button or a command button (by default it is a

submit button)

The Button Web Control (Page 2)

• A “submit” button does not have a command name and it posts the page back to the

server when clicked

– It is possible to write an event handler to control the actions performed when the

submit button is clicked

• A “command” button has a command name and allows the creation of multiple Button

controls on a page

– It is possible to write an event handler to control the actions performed when the

command button is clicked

The Button Web Control (Page 3)

• Format:

<asp:Button ID="ButtonID" runat="server" Text="Button Text" />

– The Text property is the label displayed on the button

– A one-sided tag—requires tag end character (/) inside it

• Example:

<asp:Button ID="ButtonInsertPublisher" runat="server" Text="Insert Publisher" />

The Button Web Control (Page 4)

• The OnClick property links a Button to a Click event handler (a method)

• Format:

<asp:Button ID="ButtonID" runat="server" Text="Button Text"

OnClick="MethodName" />

• Example:

<asp:Button runat="server" ID="ButtonInsertPublisher" Text="Insert Publisher"

OnClick="ButtonInsertPublisher_Click" />

Events and Event Handlers (Page 1)

• For almost every object on the Form, the browser can respond to many mouse and

keyboard actions

• Some examples:

– Click, MouseDown, MouseMove, KeyPress, Validating, Validated

• The event handler for the object and its related event appears as a method within the

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

92

93

94

95

96

Page 16: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 16

– Click, MouseDown, MouseMove, KeyPress, Validating, Validated

• The event handler for the object and its related event appears as a method within the

.aspx.cs file (the C# code for the form)

Events and Event Handlers (Page 2)

• To create event handlers for any control double-click on the object

– Default event for an asp:Button is Click

• For each event handler method, this “registers” an OnClick property within the control

that links it to the method, e.g.:

<asp:Button runat="server" ID="ButtonInsertPublisher" Text="Insert Publisher"

OnClick="ButtonInsertPublisher_Click" />

Syntax of an Event Handler Header (Page 1)

• Format:

protected type methodName(object sender, EventArgs e)

– methodName by default consists of the concatenation of the object name and event

type

• Event handler methods have two parameters:

– sender: a reference which identifies the object (control) which initiated the call to

the method

– e: an object variable that stores property values and methods related to the event

Syntax of an Event Handler Header (Page 2)

• Format:

protected type methodName(object sender, EventArgs e)

{ …

• Example:

protected void ButtonInsertPublisher_Click(object sender, EventArgs e)

{ …

Manually Insert Values into a SqlDataSource Parameter

• Assign values to the individual InsertParameters in the collection using the

DefaultValue property

• Format:

SqlDataSource.InsertParameters ["ParameterName"].DefaultValue = value;

• Example:

SqlDataSourceInsertPublisher.InsertParameters["PublisherCode"].DefaultValue =

TextBoxPublisherCode.Text;

The SQL INSERT Command (Page 1)

• Adds one record to a database table:

• Format:

INSERT INTO TableName

VALUES (value1/"string1" [, value2/"string2", … ])

– The number of fields and data type (including in order) must match those in the

database table

– value(s) may be numeric, Boolean, Date, etc

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

97

98

99

100

101

Page 17: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 17

database table

– value(s) may be numeric, Boolean, Date, etc

The SQL INSERT Command (Page 2)

• Example:

INSERT INTO Book

VALUES ("7711", "Of Mice and Men", "BB", "FIC", 10.99, .F.)

The SqlDataSource Insert() Method

• Inserts a new record into the table represented by a SqlDataSource control

– A SelectCommand must have been configured

– Values must have been previously assigned all the InsertParameters for fields that

require a value

• Format:

SqlDataSource.Insert();

• Example:

SqlDataSourceInsertPublisher.Insert();

The Response.Redirect Method

• Causes the browser to redirect the client to (load) a different URL (web address)

instead of reposting to itself

• Format:

Response.Redirect("WebAddress");

– The WebAddress is a URL passed as a string

• Examples:

Response.Redirect("PublisherTable.aspx);

Response.Redirect("http://www.profstruck.net");

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

102

103

104

The asp:DetailsView Web Control (Page 1)

• The asp:DetailsView displays the values of a single record one at a time from a data

source

• Each row in the DetailsView control represents one field within the record

• Format:

<asp:DetailsView ID = "ControlName" runat = "server"> </asp:DetailsView>

The asp:DetailsView Web Control (Page 2)

• To assign the data source:

– Click the “smart tag” and select a data source (e.g. a SQLDataSource object) from

the Choose Data Source: drop-down list

• The rows are formatted automatically to represent the fields of the selected query

(data source)

• The source code is updated automatically to include a <Fields> block and

<asp:BoundField> tags which represent the data

The asp:DetailsView Web Control (Page 3)

• When a data source is selected for the DetailsView control, the following of its

properties usually are updated automatically:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

105

106

107

108

Page 18: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 18

• When a data source is selected for the DetailsView control, the following of its

properties usually are updated automatically:

– AutoGenerateColumns—how are fields generated? (set to False):

• True—generated from the data source at run-time

• False—permanently defined in the <Fields> block

The asp:DetailsView Web Control (Page 4)

• When a data source is selected for the DetailsView control, the following of its

properties usually are updated automatically (con.):

– DataKeyNames—the DataField property of the BoundColumn(s) that uniquely

identify each record (by default the primary key)

– DataSourceID—the ID of its data source controls

The asp:DetailsView Web Control (Page 5)

• Formatting for the DetailsView control is similar to that of the GridView

• Easiest to use AutoFormat

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

109

110

SqlDataSource: ORDER BY

• When configuring a SqlDataSource control, at the “Configure the Select Statement”

dialog window:

– Click the <ORDER BY…> button to get to the Add ORDER BY Clause dialog box for

sorting the result of a SQL SELECT statement

– Specify up to three columns (fields) to sort the results

• Second and third columns used for intermediate and minor level sorts within the

major sort

– To change sort order for any column to descending, select Descending option

(ascending is default)

The asp:DropDownList Web Control (Page 1)

• The asp:DropDownList Web control creates a Form field that allows users to select

from drop-down lists of multiple options

• Format:

<asp:DropDownList ID = "ControlName" runat = "server"> </asp:DropDownList>

The asp:DropDownList Web Control (Page 2)

• The list (an Items collection) can be populated in from a data source:

– Assign the asp:DropDownList’s DataSourceID property to a SqlDataSource object (or

some other data source)

– The easiest way is to use the “smart tag” (just like GridView and DetailsView)

The asp:DropDownList Web Control (Page 3)

• Properties for the asp:DropDownList Web control with a data source:

– DataSourceID—name of the SQLDataSource (or some other data source) that

provides the items for the list

– DataTextField—column from the SELECT list of the data source that is the text

displayed for each item

– DataValueField—column from the SELECT list of the data source that is the value

(text) passed to the server for processing when an item is selected

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

111

112

113

114

115

Page 19: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 19

– DataValueField—column from the SELECT list of the data source that is the value

(text) passed to the server for processing when an item is selected

Query Strings (Page 1)

• A query string is part of a URL (web address) that used to send the values from one

webpage to other page (like passing arguments)

• Consists of a series of “key and value” pairs

– The key is the “variable” name

– The value is a value assigned (=) to the key

• Multiple keys and values may be joined by using the ampersand (&) character and

passed together in the same URL, e.g.:

– key1=value1[&key2=value2 …]

Query Strings (Page 2)

• In a URL the key and value pairs follow the Web address and a question mark (?)

symbol:

http://WebAddress?key1=value1[&key2=value2 … ]

– Each of the key and value pairs are separated by an ampersand (&) symbol

Query Strings (Page 3)

• Example:

http://www.MyWebSite.com/PublisherDetail.aspx?PublisherCode=AH

• Example when the file has the same path (location):

PublisherDetail.aspx?PublisherCode=AH

Passing a Query String Through the Response.Redirect Method

• A query string may be included within the string argument of the Response.Redirect()

method

• Format:

Response.Redirect("WebAddress?key1=value1[&key2=value2 …" ]);

• Examples:

Response.Redirect("http://www.MyWebSite.

com/PublisherDetail.aspx?PublisherCode=AH");

Response.Redirect("PublisherDetail.aspx? PublisherCode=AH");

The SelectedValue Property for the DropDownList Control

• The SelectedValue property represents the text assigned to the Value property for the

currently selected item of the DropDownList control

• Format:

DropDownListControlID.SelectedValue

• Example:

DropDownListPublisher.SelectedValue

Using the SelectedValue in a QueryString

• The SelectedValue property of a DropDownList may be used to customize the value

element in a query string

• Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

116

117

118

119

120

121

Page 20: CST141 ASP.NET Database Page 1 · 2018-04-21 · CST141—ASP.NET Database Page 1 ASP.NET Database CST242 Database •A database (the data) –A computer filing system •I.e. Payroll,

CST141—ASP.NET Database Page 20

element in a query string

• Example:

Response.Redirect("PublisherDetail.aspx?PublisherCode=" +

DropDownListPublisher.SelectedValue);

QueryStringParameters in SELECT Command of SqlDataSource (Page 1)

• A QueryStringParameter for a SQL SELECT command automatically retrieves values

from the query string

• When configuring the SqlDataSource:

– Click the <WHERE…> button and select the Column (field) for the WHERE clause

and the Operator

– From the Source drop down list select “Query String”

– Enter the key name from the query string into the QueryString field textbox

QueryStringParameters in SELECT Command of SqlDataSource (Page 2)

• Process inserts an asp:QueryStringParameter into SqlDataSource’s <SelectParameter>

block

– As opposed to an asp:InsertParameter

• Sets its QueryStringField property to the query string key

• Additionally inserts a WHERE clause with the parameter into the SELECT statement

QueryStringParameters in SELECT Command of SqlDataSource (Page 3)

• Example:

<SelectParameters>

<asp:QueryStringParameter Name="PublisherCode"

QueryStringField="PublisherCode"

Type="String" />

</SelectParameters>

The Request.QueryString Method

• Retrieves (returns) the collection of key and value pairs from an HTTP query string

• Format:

Request.QueryString["keyName"]

– The keyName is a string which is the name (key) as assigned when the original query

string created by the Response.Redirect method

• Example:

String publisherCode = Request.QueryString["PublisherCode"]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

122

123

124

125