COP2360 – C# Programming Chapter 15 – Web Based Apps Announcements Final Exam will be April 29 (Instead of May 6) – – Will be comprehensive Will have a short review the hour before it starts Our In-Class Project Design and Develop a Moving From Web Page to Web Page Hyper Link Fields can be set up in your forms that do this automatically You can also do this programmatically with – – String sURL = "~/FormName.aspx Response.Redirect(sURL); The Session Object Each Web Page is autonomous – – When it starts up, it knows nothing Data can be passed into the Page using the “QueryString” (see next slide) – But it must be passed each time The Session object allows one to store values for variables in a dictionary type structure, and then to retreive then when needed Session["ID"] = txtUserId.Text; String sID = (string)(Session["ID"]); QueryString Parameters String sURL = "~/FormName.aspx?Last_Name=Bradley&First_Name=Louis”; – – The “?” separates the form to load from the Query String The “&” separate different Query String Parameters String sLast = Request.QueryString["Last_Name"]; Our Design System will start up with a Logon page With a successful Logon, the system will store information about the user/student and pass off to a page that will allow the student to view available courses for a semester This form will also have links to two other forms – – One that will display all classes the student has taken in the past and their grades One that will allow the student to drop and add classes for an upcoming semester The form will also have a button to log off the user and send them back to the Logon page Our Design We are going to use both the in code connection to a database, and the data controls available in the Design View. Once we get the basics working, we are going to go back and create a Master Page and use it to standardize our project. Connection String in Web.Config Connection Strings can be stored in a file named Web.Config that is included when you publish you web application. Here’s the Local Connection String <connectionStrings> <add name=“Registration" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\REGISTRATION.mdf;Integrat ed Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" /> </connectionStrings> Connection String in Web.Config Connection String to our outside database <connectionStrings> <add name=" Registration " connectionString="Data Source=s09.winhost.com;Persist Security Info=True;User ID=DB_52704_registration_user;Password=COP2360" providerName="System.Data.SqlClient" /> </connectionStrings> Access the Connection String in Code using System.Configuration; string sConnection = ConfigurationManager.ConnectionStrings[“Registration"].ConnectionString; OK – Let’s Get Started Create Project Create Some Directories – – App-Data Images Copy Over Some File Create default.aspx Web Page Create available.aspx Web Page Code for Logon Form //string sConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; String sConnection = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\REGISTRATION.mdf;Integrated Security=True"; String sSQL = "SELECT First_Name, Last_Name, Password From Student Where Student_ID = '" + txtUserId.Text + "'"; SqlConnection dbConn = new SqlConnection(sConnection); dbConn.Open(); SqlCommand dbCmd = new SqlCommand(); dbCmd.CommandText = sSQL; dbCmd.Connection = dbConn; try { SqlDataReader dbReader = dbCmd.ExecuteReader(); if (dbReader.Read()) { String sURL = "~/Courses.aspx?LastName=" + dbReader["Last_Name"].ToString() + "&First_Name=" + dbReader["First_Name"].ToString(); Session["ID"] = txtUserId.Text; dbReader.Close(); dbConn.Close(); Response.Redirect(sURL); return; } else { lblMessage.Text = "Invalid User-Id/Password - Please Try Again"; }