Lesson 11

14
Lesson 11 Lesson 11 Adding Record To Your Adding Record To Your Database Database

description

Lesson 11. Adding Record To Your Database. Create a new page and call it AddRecords.aspx. Adding Records. Add to your workspace area. Type and center More Movie Data: On the left side of the workspace type the following: Enter Movie Name: Enter Actor: Enter Actress: Enter Director: - PowerPoint PPT Presentation

Transcript of Lesson 11

Page 1: Lesson 11

Lesson 11Lesson 11

Adding Record To Your DatabaseAdding Record To Your Database

Page 2: Lesson 11

Adding RecordsAdding Records

Create a new page and call it Create a new page and call it AddRecords.aspxAddRecords.aspx

Page 3: Lesson 11

Add to your workspace areaAdd to your workspace area

Type and center More Movie Data: Type and center More Movie Data: On the left side of the workspace type the On the left side of the workspace type the

following:following: Enter Movie Name:Enter Movie Name: Enter Actor:Enter Actor: Enter Actress:Enter Actress: Enter Director:Enter Director: Enter Release Date:Enter Release Date: Enter Rating:Enter Rating: Enter MPAA:Enter MPAA: Enter Run Time:Enter Run Time:

Page 4: Lesson 11

Add TextboxAdd Textbox Drag a TextBox and place it to the right of field Drag a TextBox and place it to the right of field

names you just entered (Use Absolute names you just entered (Use Absolute Position found under the Layout menu for Position found under the Layout menu for placementplacement

Page 5: Lesson 11

Add a buttonAdd a button

Drag a button and place in the middleDrag a button and place in the middle

Page 6: Lesson 11

Label MessageLabel Message

Drag a Label and place Drag a Label and place it below the buttonit below the button

On the label properties, On the label properties, do the following:do the following:

To the right of ID type To the right of ID type labelMessagelabelMessage

Select Fore Color to be Select Fore Color to be RedRed

Aside of Text Delete the Aside of Text Delete the word Labelword Label

Page 7: Lesson 11

Adding ButtonEvent CodeAdding ButtonEvent Code

Click on Code ViewClick on Code View Drag an INSERT Data MethodDrag an INSERT Data Method Select the Movies Database and click OKSelect the Movies Database and click OK Click Next and name the Method Click Next and name the Method

AddMovieRecordAddMovieRecord

Page 8: Lesson 11

Insert CodeInsert Code

Go back to Design View Go back to Design View and double click the and double click the buttonbutton

Before the End Sub typeBefore the End Sub type Dim MovieName As Dim MovieName As

StringString Dim Actor As StringDim Actor As String Dim Actress As StringDim Actress As String Dim Director As StringDim Director As String Dim ReleaseDate As Dim ReleaseDate As

DateTimeDateTime Dim Rating As StringDim Rating As String Dim MPAA As StringDim MPAA As String Dim RunTime As StringDim RunTime As String

You have just created You have just created variables for all your variables for all your fields used in the fields used in the databasedatabase

Page 9: Lesson 11

More Code To AddMore Code To Add

MovieName= Server.HtmlEncode MovieName= Server.HtmlEncode (TextBox1.Text)(TextBox1.Text)

Actor= Server.HtmlEncode (TextBox2.Text)Actor= Server.HtmlEncode (TextBox2.Text) Actress= Server.HtmlEncode (TextBox3.Text)Actress= Server.HtmlEncode (TextBox3.Text) Director= Server.HtmlEncode (TextBox4.Text)Director= Server.HtmlEncode (TextBox4.Text) ReleaseDate= Server.HtmlEncode ReleaseDate= Server.HtmlEncode

(TextBox5.Text)(TextBox5.Text) Rating= Server.HtmlEncode (TextBox6.Text)Rating= Server.HtmlEncode (TextBox6.Text) MPAA= Server.HtmlEncode (TextBox7.Text)MPAA= Server.HtmlEncode (TextBox7.Text) RunTime= Server.HtmlEncode (TextBox8.Text)RunTime= Server.HtmlEncode (TextBox8.Text)

Page 10: Lesson 11

Server.HtmlEncode CoversionServer.HtmlEncode Coversion

For the variables you created you must then For the variables you created you must then pass the information to those variables with pass the information to those variables with this conversionthis conversion

Remember the AddMovieRecord method you Remember the AddMovieRecord method you created? This is now going to be used in your created? This is now going to be used in your codecode

Page 11: Lesson 11

Passing Values To Your DatabasePassing Values To Your Database

Continue to type this code:Continue to type this code: TryTry AddMovieRecord (MovieName, Actor, Actress, AddMovieRecord (MovieName, Actor, Actress,

Director, ReleaseDate, Rating, MPAA, Director, ReleaseDate, Rating, MPAA, RunTime)RunTime)

labelMessage.Text= "Your movie record has labelMessage.Text= "Your movie record has been updated, Thanks"been updated, Thanks"

Button1.Enabled = FalseButton1.Enabled = False CatchCatch labelMessage.Text= "Please submit again labelMessage.Text= "Please submit again

there was an error"there was an error" End TryEnd Try

Page 12: Lesson 11

Error Checking CodeError Checking Code

The Try-Catch block is for error checkingThe Try-Catch block is for error checking You can see the two messages that were You can see the two messages that were

created and will be displayed in entries were created and will be displayed in entries were successful or unsuccessfulsuccessful or unsuccessful

Page 13: Lesson 11

Entering RecordsEntering Records

Press F5 and load the new pagePress F5 and load the new page Enter the following record to your Movie Enter the following record to your Movie

databasedatabase

Page 14: Lesson 11

UpdateUpdate

You can see that your movie record has been You can see that your movie record has been updated successfullyupdated successfully

For now you can go back to Web Matrix and For now you can go back to Web Matrix and click on data and view the updated click on data and view the updated JamesBondSeries Table with you your new JamesBondSeries Table with you your new recordrecord