1 / 34

ASP Chapter 8

ASP Chapter 8. Active Server Pages. Server-side technology Enables dynamic , interactive , data-driven web pages Scripting OR compiled languages VBScript, VB, C# Jscript, Java Perl Etc… HTML for display/gathering of data ADO for access to data. ASP vs. ASP.net.

makoto
Download Presentation

ASP Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASPChapter 8

  2. Active Server Pages • Server-side technology • Enables dynamic, interactive, data-driven web pages • Scripting OR compiled languages • VBScript, VB, C# • Jscript, Java • Perl • Etc… • HTMLfor display/gathering of data • ADOfor access to data

  3. ASP vs. ASP.net

  4. Internet Resources • ASP.net • http://www.w3schools.com/aspnet/  Good for syntax • http://www.codefixer.com/asp-net/tutorials/ • http://www.dotnetspider.com/tutorials/AspNet-Tutorials.aspx • http://www.learn-asp.net/asptutorials/default.aspx • http://vishaljoshi.blogspot.com/2009/05/creating-simple-net-40-web-app-using.html • http://www.asp.net/web-forms/overview/getting-started/intro-to-aspnet-controls  Basic ASP.net Controls • http://msdn.microsoft.com/en-us/library/tw738475.aspx  Setting up an ASP.net Data Control (Gridview), plus others • http://www.extremeexperts.com/Net/FAQ/DiffBetweenServerandHTMLControls.aspx  Difference between ASP.net controls and HTML controls • HTML • http://www.htmlgoodies.com/primers/html/article.php/3478131 • http://www.w3schools.com/html/default.asp

  5. Basic Steps • New ASP.net project • (Optional) Create or Customize Site.Master template • (Optional) Create or Customize Default.aspx page • Create New webpage (.aspx) files • Labels • Data Controls • Buttons • Create Code-Behind (.vb) files • Database access, data manipulation • Test/Debug

  6. Database Access Steps • Define program variables for data access objects • Connection Dim connObject = New SQLClient.SQLConnection(…..) • Query / Command Dim cmdObject AS New SQLClient.SQLCommand (….., …..) • Data Container Dim drObject AS SQLClient.SQLDataReader • Data Reader • Data Adapter… • Create connection to the database • Open connObject.Open() • Run query/command and store data in container • Execute drObject = cmdObject.ExecuteReader • Display, Maintain data via Data “Container” Controls • Grid View • Details View • Form View • List View… • Release objects • ClosedrObject.close() connObject.close()

  7. Create New ASP.net Project

  8. Customize Environment

  9. Customize Site.Master template(image here: http://business.baylor.edu/gina_green/computer.jpg)

  10. Customize Site.Master template, cont…(example images here: http://www.baylor.edu/about/?_buref=1155-90749)

  11. Example 1: View Student Info: Create webpage • Prompt user for search criteria • Need to know exact data values • Find student information based on search criteria

  12. Example 1: Create webpage, cont…

  13. Example 1: Create webpage, cont…

  14. Database Access Code 'THIS CODE GOES IN THE VB FILE for the View Student Information button lblStatus.Visible= False '(1) create an ADO connection object to establish connection to the database 'first define a text string that describes database connection information Dim connectionstring As String = "server=hsb-mis-sql; database=mis4340; integrated security=sspi" 'create the ADO connection object that will establish a connection to the database Dim myConnection = New SqlClient.SqlConnection(connectionstring) '(2) make the connection to the database myConnection.Open() '(3) setup queries that get student data from database based on values the user entered Dim querystring1 As String = "select * from student where stud_id='" & txtStudentID.Text & "';" Dim querystring2 As String = "select * from student where firstname like '%" & txtFirstName.Text & "%' and lastname like '%" & txtLastName.Text & "';" Dim querystring 'Decide which query to use based on which values the user entered If (txtLastName.Text <> String.Empty Or txtFirstName.Text <> String.Empty) Then querystring = querystring2 Else querystring = querystring1 End If '(4) create an ADO command object that will contain the query command to be executed by the database Dim myCommand As New SqlClient.SqlCommand(querystring, myConnection) '(5) create an ADO container object that will hold data retrieved from database Dim myDatareader As SqlClient.SqlDataReader '(6) execute the command and store results in the datareader container object myDatareader = myCommand.ExecuteReader 'if datareader container is empty, display error message If Not myDatareader.HasRows Then lblStatus.Text = "No Student Found" lblStatus.Visible = True myDatareader.Close() Exit Sub End If '(7) close objects myDatareader.Close() myDatareader = Nothing myCommand = Nothing myConnection.Close() myConnection = Nothing

  15. Database Access Code, cont… 'THIS CODE GOES IN THE VB FILE for the CLEAR button Response.Redirect(Request.Url.PathAndQuery, True)

  16. Example 2: View Student Info: add Data Controls • Display student information based on search criteria • Use GridView data control for displaying Student data

  17. Configure the Gridview Control

  18. Data Bind/Display Code 'THIS CODE GOES IN THE SAME VB CODE for View Student Information button …..'(7) bind control to data source, then display data to user StudentGrid.DataSource = myDatareader StudentGrid.DataBind() StudentGrid.Visible = True

  19. Example 3: View Student Info: add Data Controls, cont… • Prompt for search criteria • Use Dropdown web control for specifying Student ID • See http://www.dotnetcurry.com/ShowArticle.aspx?ID=221 for how to implement “cascading dropdowns” for Lastname and Firstname

  20. Wizard: Configure the Database Connection

  21. Wizard: Configure the SQL Command Will give us a field which is the concatenation of student ID and first/last name for ease of lookup Will give us student ID as the key field

  22. Wizard: Bind Data to Dropdown Name of the data source (connection and query) we just created The field from the query that we want displayed in the dropdown The field from the query that we want to use as the selected value from the dropdown

  23. Result: ASP.net Generates "Code" to Access Data Will be used as the Data Container object and dropdown control will be set to its values Will be used by a Command object Used DropdownList control & configured its data source via Wizard Will be used by Connection object Ensure DropDownList control has this name

  24. Modify Database Access Code • In VB Code for View Student Information button, retrieve the selected student from the new dropdown instead of from the text box • i.e., replace txtStudentID.Text with SelectedStudentID.Text

  25. Example 4: View Student Info: add Data Controls, cont… • Add dropdown option for "All Students"

  26. Modify Database Access Code • Add highlighted code to the aspx file: <asp:DropDownList ID="SelectedStudentID" runat="server" DataSourceID="SqlDataSource1" DataTextField="studentinfo" DataValueField="stud_id" AppendDataBoundItems = "true"> <asp:ListItem Selected="True" Value="-1">All Students</asp:ListItem> </asp:DropDownList> • In the “View Student Information” button VB code, add third querystring to select all rows and columns from student table ***On your own *** • Also in the “View Student Information” button VB code, update IF-Then-Else in VB code to check for "-1" value (i.e., user selected “All Students”) and assign appropriate query ***On your own *** NOTES: Put -1 in quotes (e.g., “-1”); Use ElseIf after IF and before ELSE

  27. Example 5: Edit Student Info • Edit student information based on a specific student selected • Will use Detailsview Control for updating of data ***could have used Gridview or another control as well***

  28. Edit Webpage

  29. Wizard: Configure SQL Command

  30. Set Additional Properties Put a button in the data control that allows user to begin editing record Hide control that allows user to Edit until user clicks the Edit Student Information by StudentID button FindStudent.aspx Prevents user from editing the Stud_ID (primary key) field

  31. Data Bind/Display Code 'This code goes in the same VB code for View Student Information button 'add the highlighted line to step (7) as follows: '(7) bind control to data source, then display data to user StudentGrid.DataSource = myDatareader StudentGrid.DataBind() StudentGrid.Visible = True btnEditStudent.Visible = True 'This code goes in the btnEditStudent_Click Event VB code 'Hide the StudentGrid control StudentGrid.Visible = False 'Bind control to its data source, then display the EditStudentDetails control for editing EditStudentDetails.DataBind() StudentGrid.Visible = False EditStudentDetails.Visible = True btnEditStudent.Visible = False

  32. Example 6: Navigation • Update Site Master template to add, change, and/or remove menu items—be sure to change the href and the text

  33. Misc Tips (Contact Page) • Make an email address clickable (assume column named "email") • Display image (assume filename stored in column "photofile") • Display an image (assume column named "photofile" stores full pathname of file containing the image)

  34. Summary • What is ASP? • Benefits of ASP.net • Key objects involved in database access • Key steps involved in database access • Creating a simple data-driven web application • Retrieving data from database • Editing data via data controls

More Related