Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Open Database Connectivity wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Functional Database Model wikipedia , lookup
Relational model wikipedia , lookup
ContactPoint wikipedia , lookup
COMP3121 Week 4: Writing Data to a Database with DataView This week’s session will explore the concepts of writing data to a database, and updating existing records in a database, via web pages and asp.net controls. A lot more can go wrong with writing to database than with reading from database. All the field settings in the database have to be consistent with those on the corresponding .aspx page to accommodate the data sent from the data form. Also, the database has to have the right file permissions to be able to receive data. As with displaying data, an ObjectSource control is always required, and should be set up first to establish the connection with the database, and create a connection ID. Exercise 4(a): Using DetailsView to Insert Data As last week, first create a new content page and bring in the controls… 1. Open your website from last week. Take a look at the various pages. If your master page was behaving strangely, perhaps this is an opportunity to create a new one. Up to you… (good practice, anyway!) 2. As before, click Add…. New Item…, then Web Form, and add a suitable file name e.g. NewProducts1.aspx. Associate this with a working (ie no build errors) master page…, and create the page. 3. Switch to Design view. Type in Product Information as a title, select the text, and then format the text as, e.g., heading 4. Make sure your Access database from last week is stored in the Web site's App_Data folder (In addition to the reasons already stated for using App_Data, Visual Studio’s solution management ensures that, for local testing purposes, the Web server has permissions to read and write. Last week, you may have noticed that the object source wouldn’t even display the database until it had properly been “included” with the project. For Web sites on remote computers, database permissions such as “write” may need to be added separately. More on this later.) 5. You may have noticed last week that a number of Toolbox controls are available in the Data group. This time, drag a DetailsView control onto the page within a “div” section. 6. Now, use the tag to configure and link to your existing database using a <new DataSource> control from within DetailsView. RCH11 1 7. The wizard will now display a window where you can specify what data you want to retrieve from the database, either using SQL SELECT for reading data from one or more tables (as was the case last week), or SQL INSERT, UPDATE, DELETE commands, which you will have come across in other modules. On the Configure the Select Statement page, select Specify columns from a table or view, and then in the Name box, click Products (or whatever you called the table), and make sure all fields are highlighted. Click Advanced 8. Click on the Generate INSERT, UPDATE, DELETE statements box and clock OK. 9. Now click on the specify an SQL statement button, then Click on Next. You should see options for SELECT, UPDATE, INSERT, DELETE. Click on OK/Next 10. You will be presented with a “Query Builder” option at this point. Don’t use it (a query has already been generated). Now click Test Query to preview the data (i.e. in SELECT mode), and then click Finish. 11. Back in design view, Right-click the DetailsView control, click Properties, and set AllowPaging to true. This will allow paging through individual product entries when they are displayed. 12. Also check the box to allow insert mode. Now go back to source view and check the code. Notice that in addition to the display form you created last week, there is also an option to INSERT, UPDATE (EDIT) or DELETE records. There is also a glitch in the code, bearing in mind what is normally required for an INSERT record process! Take a good look at the SQL queries and see if you can spot it! If you think you are onto something, try running the file and attempting to add a NEW record, to confirm your suspicions. An error should be generated, saying something about a NULL value. Is this consistent with your predictions? Don’t worry if you can’t make sense of it… all explained below. 13. As it stands, the INSERT command is trying to write a PRODUCTID (or whatever you chose for the primary key) value to the database. However, as you’ll see, there is no field to enter productID. This is correct, because the ID will be added automatically by an “autonumber” field at the database end… To correct this inconsistency, remove the primary key field from the INSERT statement, and also remove the ?, component that relates to this field. Also, it is necessary to remove the productID line from “Insert Parameters”. Resave the modified file. 14. Now, Run the page... again if you tried it before. As you can see the additional feature to last week’s display is that you should notice a “New” option under the record displayed. (PTO) RCH11 2 15. Once you’ve clicked New, an option to enter new data (no primary key… why?) is presented. Add the data, and click on Insert… Rejoice! You should now have an extra record in your database. 16. If you don’t like how the page is displayed, go back and use the Autoformat in Details View to improve the presentation. This shouldn’t affect the data displayed, because that still comes from the same source, using the same SQL statement. Better? Well done! But that’s not all…. Exercise 4(b): Using DetailsView to Update/Delete Data Once you have successfully written a record to a database table, you should find it a relatively easy task to modify the control so you are able to update or insert existing records. 1. Go back into design mode, and click again on the smart tag. Select both update (edit) and delete record options, and uncheck the insert option. 2. Resave the file with a new name, updatedelete.aspx, and run the newly created file. 3. Now test it out by choosing a record, and attempting to change its price. Once finished… are you sure? If so, click on update (ie edit), and (provided write permissions are effective – they should be!) the record will be permanently changed in the database. 4. Now scroll to a record you are prepared to delete. Again, “are you sure?” Click on delete, and it’s gone! So far, so good. Now you really are getting to grips with database interaction… Save All, and close your project. Important Note: It wouldn’t be a good idea for customers to be able to change the price of a product – for this reason, a file that can editing records should be kept well away from the customer… Exercise 4(c) A Data Table for Customer Data Any online shopping system has to provide a facility for customers to enter their personal data. This exercise will allow customer data to be collected, using the existing database file… RCH11 1 Open your existing Access Database file 2 Set up a second Data Table called Customers 3 Set up a structure for the table with CustomerID (autonumber) as the Primary Key, and other fields as text to include 3 representations of the FirstName, Surname, email_address, telephone, postcode, username. Finally, custpassword (password is a reserved word!) needs to have the data masked. No image fields needed on this occasion! 4 Now introduce data for two fictional customers, and save to the database to your App_Data folder as before. 5 Load up the Visual Studio environment, and re-open the site you have been building. 6 Create a new dynamic web page with the name customers.aspx (as before, if you just type the name i.e. customers, the suffix will be added automatically). 7 Now, as in Exercise 4(a), use DetailsView and AccessDataSource to create the code that will allow the display, editing and deletion of existing data (don’t forget to use the customers table for your queries, and do include paging!). As before, you can use DetailsView’s autoformat facility to make the presentation of data acceptable. . 8 Finally, save and run the file to make sure you can view existing data from the customers table. 9 Now create another .aspx page for adding a customer record. Call it newcust.aspx, and again use the Ex. 4(a) technique. Does it work? If not, remember the modification you had to add last time… if you remembered to do that and it still doesn’t work, seek help. Otherwise, well done, excellent progress. That’s all for today! Summary DetailsView can be used for the full range of database operations, so this tends to be a popular control. However, despite to autoformatting options, it does have its limitations regarding screen design, and a knowledge of HTML is required to create professional-looking pages. Next Week… So far, you have created two isolated tables. Relational databases are all about joining data together, so we’ll see how that can be achieved within a dynamic web environment, in the context of a shopping cart system. RCH11 4