Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
A nice improvement for our application would be to store our searches in a database. Visual Studio contains a number of useful data tools. We'll look at the Data Sources window, and the drag-and-drop binding in his movie. I'm inside Visual Studio. I've opened the Solution called InfoReaderData, which contains one project called InfoReaderV4. I've got this MainWindow.xaml file, which is a continuation from the other movies in this chapter. I'm going to take the information when you click on either of these search buttons, and I'm going to store it in the database.
Then I'm going to show you your history of your searches over in this list box on the History tab. The first thing I need to do is add a database. To do that, I'm going to go to my project, right-click, and choose Add > Existing Item. Then I'm going to navigate out to my Desktop, find the Exercise Files folder, and look in the Assets folder. Be sure and change this dropdown to show All Files. For today, I'm going to use a compact SQL database.
Then name of this database is History.sdf. So I'll choose this one and click on Add. Now Visual Studio realizes that I've picked a data source. It's asking me if I want to make a special type of data access tool called a DataSet. I'm going to go ahead and say Next on this. Visual Studio then queries my database and shows me a list of all of the tables. In this example, I only have one table with two columns. I will check this first check box, and then verify that I like this DataSet name down here.
If I do, I'll click on Finish. A number of things happened when I click the Finish button. First of all, it made a copy of the History.sdf file in my project. It also added a connection to my Server Explorer. If you don't have Server Explorer opened, open it now. You can find it in the View menu or dock to the side of your window. You can see I now have this History.sdf connection. If I expand this, I can see the tables inside that database. I can even right-click on this History table and choose Show Table Data.
Currently, I have no rows in this table, so let me put two rows in there. I'll come over to this column, and type in "WPF", and hit Enter. Then I'll type in "lynda.com". Press Enter. Then I'll close this window. Not only did it add the database, but it also generated this HistoryDataSet.xsd file, and all of its companion files. Let me double-click on the HistoryDataSet, and show you the designer. This is the TableAdapter that is responsible for loading the data into my table from the database and sending the updated data back to the database.
So you see it has a Fill method. I'm going to use that to get the data. To put a new row in the DataSet, I'm going to add a brand-new query. I'll right-click and choose Add Query. Then I'll click on Next. Then I'm going to come down here and choose Insert. It adds a new row to the table. I'm going to click on Next. I'm going to go ahead and accept the defaults. This is the SQL statement that'll be run against the database. Just choose the defaults and click on Next. Then I get to name my query. Again, I'm going to choose the defaults, InsertQuery.
Now when I'm finished, I click on the Finish button. You'll see at the bottom of the TableAdapter is now InsertQuery. Then the @p1 means that I have to pass some sort of parameter into that query. Now I am ready to do some data binding. Let me switch over to my MainWindow.xaml file. Switch to the History tab, and select the list box. I'm going to click on this Zoom to Fit to make it a little bit easier to see. There is a special window for drag-and-drop data binding available inside Visual Studio.
It's in the Data menu. It's called Show Data Sources. I'm going to go ahead and click on this stick pin, the Auto Hide button. On my machine, it docks itself to the bottom left-hand side of the window. Notice that my DataSet is showing up here, and the table that's inside my DataSet is showing up there as well. Now for drag-and-drop data binding, I can pick this table up and drag it over and drop it on my list box. Before I do though, let me point out my XAML that I have down here. It's only got a few items in here.
Keep an eye on that when I drag this over and drop it on the list box. You see what happened is that Visual Studio added three new lines. It wrote a DisplayMemberPath, what to show; it'll show this column called SearchText. It shows that it's bound to a source. This is the WPF and Silverlight way to set up bindings. This tells WTF to monitor the underlying source and load this list box with whatever data is inside this DataSet. Then I also have a SelectedValuePath for this field here, HistoryID.
That's in case the user selects an item. I can use this as the identifier of the row that they selected. I'm not going to be using that in my demo. I'll choose Debug > Start Debugging, save my changes. Then I'm going to click on the History tab. There, I can see my two items from my database. So I've successfully set up a data binding. The next part of the demo will be when the user types in a string here and clicks on of these buttons, I want to add it to the database and make sure that this list box updates.
To do that, I'm going to switch to the code behind. While I'm inside my designer, I can press F7, which switches me over to the Code view. Now when I did the drag-and-drop binding, Visual Studio wrote a lot of code in the Window_Loaded event. It's very hard to read. So I'm going to replace this with a slightly more readable version. I'm going to delete the code here, by pressing Delete. Then I'm going to take this prewritten code here and cut it and paste it right above the Window_Loaded event.
Then I'm going to uncomment the code by pressing Ctrl+K, Ctrl+U. This is going to create an instance of the DataSet. It's also going to create an instance of that TableAdapter that knows how to load and update and insert my data. I'm going to do that here because I'm going to use it elsewhere in my code. So I'm doing it outside the boundaries of this Loaded event. Then I'm going to take this commented code down here and cut it, Ctrl+X, and paste it in the Loaded event. Then uncomment it, Ctrl+K, Ctrl+U. What this does-- now, I see I have an error here.
I'll click here, and then do a Ctrl+Period. Get the smart tag, and then say, using System.Windows.Data. That will fix my code. What this does is it looks in my XAML, and finds something that was created by Visual Studio called HistoryDataSet. Then it creates a TableAdapter, instantiates one, fills the data set, and then sets up something special called a CollectionViewSource, and then moves to the first row. When I call the SendToBing method, I'm going to insert a new row by calling the ta.InsertQueryp-- that's the query we built earlier today.
We're going to pass in the string from the text box, the search string. Then to make sure that it updates correctly, I'm going to call the ta.Fill, which refills the data and then rebinds to the list box. At this point, I think I'm ready to run the application. I'm going to press F5 to debug the application. Then click Yes. Verify that I only have two items in the list box. Then I'll come over here and type in Silverlight and click the Get RSS button.
There is my RSS coming back from Bing. When I click on the History tab, my new item has been added to the list box. If I were to look inside the database, I'd also see that the data is stored in the database. Now there is one more type of data binding I want to talk about in this chapter. That is called XML binding, and it's the topic of the next movie.
Get unlimited access to all courses for just $25/month.Become a member