Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now it's time to implement the functionality that will allow the user to insert a new author. In order to do this, we'll have to get our web page to accept some input and then pass that input as parameters to a stored procedure. First let's create the stored procedure that expects the parameters. I have pre-staged a stored procedure. We'll go ahead and look at that code. The procedure's name is InsertNewAuthor and accepts two parameters, a firstName parameter and a lastName parameter, and then it executes a simple INSERT statement, inserted into the Authors table, FirstName LastName and the Active status of 1. Command(s) completed successfully. That looks good.
Now back on our web page, I'll insert my cursor into the blank area under insert new author, and I'm going to type the phrase FirstName and then, I'll drag a text box from the Toolbox that will be used to insert the FirstName. I'll then put a hard Return after that and type LastName, drag another textbox, and then under all that, we'll want a single button that will cause the insert to happen.
I want to rename that first textbox, so I'll right-click on it and look at the Properties. At the very bottom of the Properties, it currently has the ID of TextBox. It's not very descriptive. I'd like it to have the ID of FirstName. and similarly the other textbox should have the ID of LastName. So that's ready to go. Now I'll double-click on the button and that'll take me to the area where I can write the code, and again, I have pre-staged some code, so I'm going to delete all of the stuff that's already there and copy and paste my own.
Looking at the top, we have several using statements that are all necessary to communicate between the web page and the database, and then we see the bulk of our work happens here in the Button_Click method. We're going to create a new connection and I'll scroll over and show you that connection is based on the ConnectionString we created previously, myDatabaseConnectionString. We'll create a new command, the CommandText will be InsertNewAuthor, the exact name of our stored procedure and the CommandType will be StoredProcedure and we'll connect the command to the connection.
Now for the parameters. We're creating two parameters: one called firstNameParam and one called lastNameParam. These correspond to the parameters the stored procedure is expecting called firstName and lastName respectively, and they will get their values from the textbox, FirstName.Text and LastName.Text. Just creating the parameters doesn't connect them to the command. So we have to do that in two additional lines of code, where we add the parameters to the command.
Then we have our typical open the connection, ExecuteNonQuery and close the connection. I've also included one more line of code to refresh the DataGrid. The DataGrid that's on the top of the screen that shows our active authors will now need to be refreshed, because we just inserted a new author. So let's test this. So for my new author, we'll do a FirstName of Steven, LastName of Jones, hit the button, and look at that, in our Active Authors grid, we now see Steven Jones.
So this proves we successfully made a round-trip from the web page, it took the parameter Steven and the parameter Jones, passed those two parameters to the stored procedure and the stored procedure executed as desired.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 99009 Viewers
80 Video lessons · 141740 Viewers
59 Video lessons · 60096 Viewers
52 Video lessons · 73248 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.