Download practical6

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

IMDb wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Concurrency control wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Functional Database Model wikipedia , lookup

Relational model wikipedia , lookup

ContactPoint wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
COMP 2121 Week 6:
Writing Data to a Database with DataView
This week’s session is to introduce you to 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 the
database. All the field settings have to be exactly right, to accommodate the
data sent from the data form. Also, the database has to have the right
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 6(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
2. Click Add New Item, click Web Form, and then in the Name box, type
NewProducts1.aspx. Associate with one of your master pages (good
practice!), and Click Add.
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. In the Toolbox, from the Data group, drag a DetailsView control onto the
page. The next step is to link it with the AccessDataSource control.
5. Right-click the DetailsView control, click Properties, and then set
AllowPaging to true. This will allow paging through individual database
entries when they are displayed.
6. Make sure your Access database is stored in the Web site's App_Data
folder to ensure that the Web server will not return the .mdb file in
response to a Web request. In addition, for local Web sites, Visual Web
Developer ensures that the Web server has permissions to read and write
files in the App_Data folder. For Web sites on other computers, you will
need to set those permissions manually.
7. Drag an AccessDataSource control onto the page. Use the tag to
configure and link to your existing database.
8. The wizard then displays a page where you can specify what data you
want to retrieve from the database, either using SQL SELECT (for reading
RCH11
1
data from one or more tables) as you will have previously done, or SQL
INSERT, UPDATE, DELETE, 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
ProductDetails, and make sure all fields are highlighted. Click Advanced
9. Click on the Generate INSERT, UPDATE, DELETE statements box
10. Click on the specify an SQL statement button, then Click on Next
11. Click Test Query to preview the data (i.e. in SELECT mode), and then
click Finish.
12. Back in design view, use the smart tag on the DetailsView control to
highlight that insert mode should be invoked. 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 or DELETE
records. There is also a glitch in the code! See if you can spot it!
13. As it stands, the INSERT command will try to write a PRODUCTID 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 by an autonumber
field at the database end… Change this, removing the field, and the ?,
component that relates to it. Resave.
14. Now, Run the page... Again, don’t worry that the image doesn’t display.
You should notice a “New” option under the record displayed. If you don’t
like how the page looks, go back and use Autoformat to improve
presentation.
15. Once you’ve clicked New, and option to enter new data is presented. Add
the data, and click on Insert… Rejoice! You should now have an extra
record in your database.
Exercise 6(b): Using DetailsView to Update/Delete Data
Once you have successfully written a record to a database table, you should
find it an 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 and delete record options, and uncheck the insert option..
2. Resave the file as updatedelete.aspx.
3. Run the newly created file. Now choose a record, and have a go at
changing its price. Once finished… are you sure? then click on update, and
the record will be permanently changed in the database.
RCH11
2
4. Now scroll to a record you are prepared to delete. Are you sure? Now, just
click on delete, and it’s gone!
Now you really are getting to grips with database interaction. There is just the
minor matter of displaying the picture files, but this is probably unnecessary
anyway when just editing records.
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 6(c): A second table for Customer Data
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
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 needed this time!
4
Now introduce data for two fictional customers, and save to the
database to your App_Data folder as before.
5
Load up the VWD environment, and re-open the site you have
been building.
6
Create a new dynamic web page that uses C# and code behind.
This helps the.net environment to add appropriate code to be
able to work with the C# server scripts that you will shortly be
using. Save this file as customers.aspx (as before, if you just
type products, the suffix will be added automatically).
7
Now, as before, use DetailsView and AccessDataSource to
create the code that will allow the display, editing and deletion of
existing data, and DataView’s autoformat to make the
presentation of data acceptable. .
8
Finally, run the script to make sure you can view existing data.
9
Now try adding a customer record. Does it work? Well done,
that’s all for today.
Summary
RCH11
3
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.
RCH11
4