Viewers: in countries Watching now:
This course investigates several key database-programming concepts: triggers, stored procedures, functions, and .NET CLR (Common Language Runtime) assemblies. Author Martin Guidry shows how to combine these techniques and create a high-quality database using Microsoft SQL Server 2012. The course also covers real-world uses of the INSERT, UPDATE, and DELETE procedures, and how to build a basic web form to connect to your database.
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.
There are currently no FAQs about SQL Server: Triggers, Stored Procedures, and Functions.
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.