12
1 Maintaining Data via a Web Maintaining Data via a Web Application Application By Susan L. Miertschin Data Access Layer Data Access Layer •Separates the data access logic from the presentation layer •Let a web service do data retrieval MAINTAIN MAINTAIN DATA DATA We looked at retrieving data. What if you need to insert, update, delete data? 3

Maintaining Data via a Web Application - University of Houston

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Maintaining Data via a Web Application - University of Houston

1

Maintaining Data via a Web Maintaining Data via a Web ApplicationApplication

By Susan L. Miertschin

Data Access LayerData Access Layer

•Separates the data access logic from the presentation layer

•Let a web service do data retrieval

MAINTAINMAINTAIN DATADATA

We looked at retrieving data. What if you need to insert, update, delete data?

3

Page 2: Maintaining Data via a Web Application - University of Houston

2

SQL SQL DATASOURCEDATASOURCE CONTROLCONTROLAbandon the Three-Tier Architecture for Now

4

Create a New Web Site ProjectCreate a New Web Site Project

Open Visual StudioFile

New Web Site

Fix Default.aspx so that “A Simple Example of Data Maintenance” displays in the title bar of the browser at run

5

Maintenance displays in the title bar of the browser at run timeFix the web.config file so that we can see error messages and turn off Windows AuthenticationDevelop locally on usb

When complete: publish to M_FCData

Connect to the Database with Visual Connect to the Database with Visual Studio’s Server ExplorerStudio’s Server Explorer

In Visual Studio – select ViewServer Explorer

You may already have a data connection to the class sql server and the FC database

6

the class sql server and the FC database that is saved in your VS ProfileIf not you may use it, otherwise, set up a connection in Server Explorer

Right Click Data Connections and select Add ConnectionUse info on next slides if you don’t remember

Page 3: Maintaining Data via a Web Application - University of Houston

3

Create the Connection to the class Create the Connection to the class SQL Server and the FC DatabaseSQL Server and the FC Database

cot-itec4339-02.cougarnet.uh.edu,18302SQL Server Authentication

logon id: WebUser or your user idPassword: is Websec18302$User for the user id WebUser (WebUser has the same

permissions to FC as does your user id)

Connection String Specifications for Connection String Specifications for the Class SQL Serverthe Class SQL Server

Use SQL Server authentication NOT Windows authentication

Login id: WebUser

8

Password: Websec18302$UserSave the password in the connection string

You would not do this in “real life”

Add ConnectionAdd ConnectionDialogue BoxDialogue Box

9

Page 4: Maintaining Data via a Web Application - University of Houston

4

Data Connection in Visual StudioData Connection in Visual Studio

In the Server Explorer you can seeTables

Views

10

Our views are under Tables

I’m not sure why

Stored procedures

User-defined functions

Not true in Spring 2009

!?!?!?!?!

Name a Table Name a Table –– Can It Be Any Table?Can It Be Any Table?

If we want to experiment with changing data in our FC database from our web app, we need to start our experimentation with a table that has no dependencies on other

11

ptables

Why?

Identify which tables are candidates for our experimentation

Tables with No FKs to Other TablesTables with No FKs to Other Tables

service_category_t

dept_ward_t

bed_type_t

d

12

medicine_t

contact_type_t

phone_type_t

staff_titles_t

certifying_board_t

Page 5: Maintaining Data via a Web Application - University of Houston

5

I CHOOSE I CHOOSE MEDICINE_TMEDICINE_TChoose one of the tables with no dependencies.

13

Add Controls to Default.aspxAdd Controls to Default.aspx

One Way: From the ToolboxDrag a GridView

Drag a SQLDataSource Control

Another Way: Drag a GridView and select New from the Choose a DataSource list

Even Another Way: Drag the medicine_t table from the Server Explorer to the webform

14All of these cause some variant of a wizard to be activated for the

purpose of telling the app what Db server and Db.

Configure the Configure the DataSourceDataSource

Whatever method you used, you have a GridView and a SQLDataSource control and you need to configure the SQLDataSource control

You are telling it what server, what authentication, what data to retrieve

15

Page 6: Maintaining Data via a Web Application - University of Houston

6

You are creating an SQL SELECT You are creating an SQL SELECT Statement HereStatement HereYou could also type it yourself: SELECT * FROM medicine_t in the query builder 16

Click on WHERE Button to Get a Dialogue Click on WHERE Button to Get a Dialogue for the Where Clausefor the Where Clause

Select the column and the operation set the comparison value to a number other than 10 (to make the exercise uniquely yours). (Pretend you are working on a report of medicines that are getting low and need to be reordered from a supplier.)

Clicking OK Takes You Back Clicking OK Takes You Back –– You Can See You Can See the SQL Being Builtthe SQL Being BuiltNow select ORDER BY and impose an artificial ordering for the data – choose whatever column you like – everyone’s can be different

Page 7: Maintaining Data via a Web Application - University of Houston

7

Click on Advanced and Make Sure the Generate Box is Click on Advanced and Make Sure the Generate Box is CheckedCheckedDecide for Yourself Whether or Not you Should Check the Other Textbox or Not

Oops! My Query Doesn’t Return Any Data!Oops! My Query Doesn’t Return Any Data!I’m going to go back through the steps and try to make it do return data

I Changed the Comparison Value to 1000 I Changed the Comparison Value to 1000 Instead of 10

Select Finish to finish.

Page 8: Maintaining Data via a Web Application - University of Houston

8

DO A TEST RUN TO DO A TEST RUN TO LOCALHOSTLOCALHOST

Run – Start Debugging – to See Data Delivered to the Page

Now We Need to Fix the App so Data Now We Need to Fix the App so Data Can Be ChangedCan Be Changed

Enable basic CRUD actions

Use the GridView Smart Tag to enable deleting and editing

23

Enable Editing Enable Editing –– Enable DeletingEnable DeletingNotice that new columns are added with Edit and Delete LinkButton Controls 24

Page 9: Maintaining Data via a Web Application - University of Houston

9

DO NOT DO NOT DELETEDELETE A VIABLE A VIABLE MEDICINE ROW UNLESS MEDICINE ROW UNLESS YOU PUT ANOTHER VIABLE YOU PUT ANOTHER VIABLE ROW BACK IN :ROW BACK IN :--))

Save – Build - Do a Test Run With Edit

I Got This Error I Got This Error –– Which is Sort of Which is Sort of Instructional Instructional –– So I’m Including ItSo I’m Including ItIf you did not get the error, then that means your version of Visual Studio 2005 is probably updated more than my version –I would appreciate someone letting me know this

26

First Note* The Error is From SQL Server Not From the First Note* The Error is From SQL Server Not From the Web Server, Per SeWeb Server, Per Se

With Web application development – when you are using server side anything – it’s important to be able to determine whether the error is cause by code you wrote –or is it a data error – or what.

27

Page 10: Maintaining Data via a Web Application - University of Houston

10

What to Do?What to Do?

I went to Help and did a search and found blogs where this bug was discussed (most likely fixed in Visual Studio 2008)

I tried some of the “fixes” I read about I tried some of the fixes I read about, but couldn’t get that to work.

Finally I went into the source view of Default.aspx and deleted the markup that created the update of the offending field (which is the one whose type is ‘text’)

See next slide

View Source of Default.aspxView Source of Default.aspxFind the SQL Update command text

29

Scroll to the right to find the clause for Scroll to the right to find the clause for the offending text field and delete itthe offending text field and delete itYou can also go into the query builder and from there delete the clause that’s causing the problem 30

Page 11: Maintaining Data via a Web Application - University of Houston

11

Keep scrolling because the comparison is Keep scrolling because the comparison is also used in the WHERE clause for the also used in the WHERE clause for the UPDATE command. Find every clause for UPDATE command. Find every clause for the offending text field and delete them.the offending text field and delete them.You can also go into the query builder and from there delete the clause that’s causing the problem 31

Should Work Now!Should Work Now!

S B ild R d Ch

Before

Save – Build – Run and Change the Data In a Row

After

Publish to M_FCData in Your Web Folder

33

Page 12: Maintaining Data via a Web Application - University of Houston

12

So So –– What if You Encounter Similar What if You Encounter Similar Problems While Building Your Project?Problems While Building Your Project?

Bummer!

Try to figure out a workaround

Ask for helpp

Use the Web

Maybe you don’t have any text fields

34

End ofEnd ofMaintaining Data via a Web Maintaining Data via a Web Application Application –– Part 1Part 1

By Susan L. Miertschin