When developing software, you often have to choose between taking a primarily “Tools Driven” approach, versus a “Maintenance Driven” approach. Simply put, Tools Driven development relies on external toolsets that make it easier to quickly write the application and get it up and running, but at the cost of understanding the system at a deep level. That’s because you’re off-loading a lot of responsibility of writing code and making relevant connections between elements in your software to the tool in question. The maintenance approach is the opposite of that. It focuses on developing a robust system that can respond to change easier because it doesn’t rely on outside tools (that might change) and is written with an understanding of how the system operates at a deeper level.
This lesson will show how a Tools Driven approach will get you up and running in a short amount of time. To start this lesson, use the LocalDbExample we left off with in Lesson 60 as the starting point, excluding the Entity model, as well as the database connection to “ACME.mdf”:
Refer again to the Data section of the ToolBox which holds a variety of ASP.NET Data Controls. There are basically two different kinds of these control:
Data Source components are not viewable to the end-user. For example, when you drag and drop a SqlDataSource onto the Design surface, it's just there to provide information to other Controls for our form:
If you click on the arrow beside the SqlDataSource and select “Configure Data Source…”, you will be taken to a setup wizard which lets you make a database connection. First, click on “New Connection”:
And from here, click on “Change”:
And set the database to a local SQL Server Database File:
Then browse to the “App_Data” folder and select the “ACME.mdf” file (local to the folder it is stored to on your computer), and then test that the connection is valid by clicking on “Test Connection”:
If you are attempting to use an older version of a SQL Server file on a newer version of Visual Studio, you may encounter an error when testing the connection. The error would say that the database file is not compatible:
If this happens, go ahead and click OK. Then, back in the "Add Connection" dialog, click OK to attempt to add this file to your project. The following message will appear:
Click Yes to confirm that you would like to upgrade the database file to the current version. However, note that this will not work in every case, but it will for this example.
If the test does resolve correctly, click on the “OK” button and continue clicking on “Next” until you get to this screen, making sure to select the Customers table and having the asterisk selected, which will select all columns in the table:
Click “Next” and on the next screen you can test a query to return results from the database:
Click on “Finish” to finalize setting up this sqlDataSource. If you go back to the markup in Default.aspx you will now see this query string that gets sent to the database whenever the sqlDataSource asked to return data:
Back in the Design view, click and drag from the ToolBox a GridView Control onto the Design surface and choose sqlDataSource1 where it says “Choose Data Source”:
It will then fill in the header information with each database column name, in the GridView:
Now when you go to run the application – without having written any code of our own – we get the same output that we got with a more hands-on approach:
You may be wondering at this point why you wouldn't use this Tools Driven approach all the time. After all, it's faster and we got the same results. The Tools Driven approach is fast but it's a bad long-term strategy. It might work for a small, departmental application where you only have a few users and you don't expect a lot of change. However, if you have a large application and you expect there to be ongoing maintenance and changes to the application over time, this is not the right approach for building that application. We looked at this approach here in order to contrast it with a better long-term approach in the next lesson. Having said that, let’s demonstrate an even easier Tools Driven approach to achieving the results we demonstrated in this lesson.
First, right-click on the project in the Solution Explorer and from the menu select:
Add > New Item
And then add a new .aspx Web Form:
Navigate to the Server Controls Window, then click and drag the Customers database table directly onto the Design surface for WebForm1.aspx:
After a moment, you will see that Visual Studio recognized what you wanted to do, and automatically set up the sqlDataSource and connected it to the GridView:
Visual Studio makes the Tools Driven approach very simple for developers who want to create a database for smaller, less complex needs. And if that's your application, then Tools Driven may be the best course for you. However, if your application is expected to need change, then it's best to steer clear of this method, even though it's tempting because of its simplicity. In the next lesson we'll learn about the Maintenance Driven approach, which will yield more benefits for developers looking to have more control over their application. Ultimately, you should learn both methods because each have their place in different cases.
Lesson 63 - Using a Tools-Centric Approach to Building a Database Application