Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 9.3 Learning Objectives Review database access in code

Similar presentations


Presentation on theme: "Unit 9.3 Learning Objectives Review database access in code"— Presentation transcript:

1 Unit 9.3 Learning Objectives Review database access in code
Using Multi-Views to Display Data Hands on exercise: U9L22

2 Data Access in Code

3 The Five Grand Steps to Database Access in Code
Get READY Add namespaces WHERE the database is Create a Connection String (name, path) WHAT needs to be done Create a Command Object (SQL) EXECUTE! Execute the Command and output data into an int Evaluate the number of records affected csFees myCommand GUI controls allow us to do a great deal but they can’t do everything .NET provides a number of objects that allow us to perform database manipulation in code The database objects are collected into three Namespaces A namespace is a way to uniquely identify a set of programming objects int intCnt = myCommand.ExecuteNonQuery(); For commands that CHANGE the database if (intCnt == 1) lblInsert.Text = “One Records was added"; else lblInsert.Text = “No records were added";

4 Execution i.e. Step #4 (in Code)
1 Execution i.e. Step #4 (in Code) Using … (namespaces) 2 string strConnection = ConfigurationManager.ConnectionStrings["cs3200"].ToString(); SqlConnection myConnection = new SqlConnection(strConnection); string strSql= "INSERT INTO OUCourses SqlCommand myCommand = new SqlCommand(strSql, myConnection); string strCName = txtCName.Text; System.Data.SqlDbType.NVarChar, 50).Value = strCName; int intCNum = Convert.ToInt32(txtHrs.Text); System.Data.SqlDbType.int).Value = intCNum; int intHrs = Convert.ToInt32(txtHrs.Text); System.Data.SqlDbType.int).Value = intHrs; int intCnt =-1 myConnection.Open(); int intCnt = myCommand.ExecuteNonQuery(); if (intCnt == 1) lblInsert.Text = “One Records was added"; else lblInsert.Text = “No records were added"; myConnection.Close(); 3a 3b 4 5

5 Multiview A set of areas whose visibility can be turned on and off to suit needs of the web page. Based on conditions Only on view is visible at a time Visible view is called the ActiveView Other Views can be added to it. Uses indices E.g. ActiveViewIndex

6 Multiview – Creating Drag a MultiView control to the web page and then
Drag one or more Views into the MultiView Name the MultiView use the prefix “mv” Name the Views use the prefix “v” ActiveViewIndex is used to display the desired View ActiveViewIndex is set to -1 to hide all views

7 Unit 9 – L2.2 For this exercise you are going to make a copy of your U9L2 application and call it lastnameU9L22 Open the page in Design view and add two blank lines after the data source that feeds the GridView Copy a MultiView to the second blank line and change its ID to mvFees Click inside mvFees and press Enter three times Copy a View to the first line in mvFees and change its ID to vAdd

8 U9L Copy another view to the second line in mvFees and change its ID to vUpdate Copy a third view to the third line in mvFees and change its ID to vDelete Click on the mvFees header, look at the properties of the multiview and set the ActiveViewIndex to -1 (meaning, don’t show any view) Your views should look something like this

9 U9L2.2 - 3 Copy a RadioButtonList to the line before mvFees
Change the ID to rblDatabaseRequests Add three ListItems to the radio button lists Change the Text and Value of the items to Add, Update and Delete Select rblDatabaseRequests and checkEnableAutoPostBack Double click rblDatabaseRequests to create a method that will change the active view Add the code to change the method based on which radio button was selected

10 U9L2.2 – 4: Move in Add Functionality
Select all the text, text boxes, buttons and validation controls you used in the L2 assignment and copy them into vAdd

11 U9L2.2 – 5: Set up Update Click in vUpdate and add text, text boxes, a button and validation similar to those found in vAdd Call the text boxes txtFeeDescription2 and txtFee2 Put all the validators in one group, different from the validation group used in vAdd

12 U9L2.2 – 6: Update Functionality
The record to update in vUpdate must come from the selected row in the GridView, when the user selects a fee. Double click on the GridView to create the gvFees_SelectedIndexChanged method Write the code shown below to copy the fee description and fee from the grid view to the appropriate text boxes. Fill in appropriate cell indices.

13 U9L2.2 - 7 What is the index of the Update command in the RBL?
If this index is selected, then before changing the ActiveViewIndex, confirm that a selection was made in the GridView. If no selection was made then set the label message to remind the user to select a fee from the table. In this case, also reset the selections of the RBL and the active views.

14 U8L Since we will need the FID in order to run the update command, reconfigure the SDS to include it. Change its visibility to false to reduce apparent changes. Double click the Update button and write code (If it reverts to the btnAdd method, delete that from the events list) Create a connection Create a command to update the fee description and fee of the record selected in the grid view Open the connection execute the command display the number of records update display any appropriate error messages Close the connection

15 U8L The code should look something like this

16 U9L2.2 – 10: Delete functionality
As before, the item needs to be selected in the GridView Add the message, ARE YOU SURE YOU WANT TO DELETE THE SELECTED RECORD, to the top area of vDelete Follow that with a RadioButtonList Set the ID to rblDelete Add two ListItems with values Yes and No Double click the rblDatabaseRequest radio button list and locate the code that checks for a SelectedIndex of 2 (Delete)

17 U9L Add code similar to what you have in the Update section – check to be sure a item is selected Double click rblDelete and write code to checks if rblDelete.SelectedIndex is 0 (yes, delete the record). Otherwise, state that no records were deleted and reset the Selected Indices for the RBL: Yes/No, RBL: data request, GV & MV

18 U9L Modify your portfolio page to link to your U8L2.2 assignment Copy your ASPPub back to ASPNET TEST, TEST, TEST to be sure all functions work correctly from ASPNET Submit the link to your portfolio page in the dropbox.


Download ppt "Unit 9.3 Learning Objectives Review database access in code"

Similar presentations


Ads by Google