Lesson 11
description
Transcript of Lesson 11
Lesson 11Lesson 11
Adding Record To Your DatabaseAdding Record To Your Database
Adding RecordsAdding Records
Create a new page and call it Create a new page and call it AddRecords.aspxAddRecords.aspx
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:
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
Add a buttonAdd a button
Drag a button and place in the middleDrag a button and place in the middle
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
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
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
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)
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
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
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
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
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