Adding Record To Your Database Lesson 11 Adding Record To Your Database
Adding Records Create a new page and call it AddRecords.aspx
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: Enter Release Date: Enter Rating: Enter MPAA: Enter Run Time:
Add Textbox Drag a TextBox and place it to the right of field names you just entered (Use Absolute Position found under the Layout menu for placement
Add a button Drag a button and place in the middle
Label Message Drag a Label and place it below the button On the label properties, do the following: To the right of ID type labelMessage Select Fore Color to be Red Aside of Text Delete the word Label
Adding ButtonEvent Code Click on Code View Drag an INSERT Data Method Select the Movies Database and click OK Click Next and name the Method AddMovieRecord
Insert Code Go back to Design View and double click the button Before the End Sub type Dim MovieName As String Dim Actor As String Dim Actress As String Dim Director As String Dim ReleaseDate As DateTime Dim Rating As String Dim MPAA As String Dim RunTime As String You have just created variables for all your fields used in the database
More Code To Add MovieName= Server.HtmlEncode (TextBox1.Text) Actor= Server.HtmlEncode (TextBox2.Text) Actress= Server.HtmlEncode (TextBox3.Text) Director= Server.HtmlEncode (TextBox4.Text) ReleaseDate= Server.HtmlEncode (TextBox5.Text) Rating= Server.HtmlEncode (TextBox6.Text) MPAA= Server.HtmlEncode (TextBox7.Text) RunTime= Server.HtmlEncode (TextBox8.Text)
Server.HtmlEncode Coversion For the variables you created you must then pass the information to those variables with this conversion Remember the AddMovieRecord method you created? This is now going to be used in your code
Passing Values To Your Database Continue to type this code: Try AddMovieRecord (MovieName, Actor, Actress, Director, ReleaseDate, Rating, MPAA, RunTime) labelMessage.Text= "Your movie record has been updated, Thanks" Button1.Enabled = False Catch labelMessage.Text= "Please submit again there was an error" End Try
Error Checking Code The Try-Catch block is for error checking You can see the two messages that were created and will be displayed in entries were successful or unsuccessful
Entering Records Press F5 and load the new page Enter the following record to your Movie database
Update You can see that your movie record has been updated successfully For now you can go back to Web Matrix and click on data and view the updated JamesBondSeries Table with you your new record