Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Integrating the Google Web Service Into ASP.NET : Page 3

Google now offers the functionality of its search engine through a Web service that offers additional search accessories and interfaces for both personal and commercial use. Learn how to incorporate Google's search power into your own applications.


advertisement
Displaying the Search Results
The final piece of the puzzle is to display the results that were found by the search. A lot of developers who use the Google Web service will return the results using one of the databound list controls such as a DataList control. My example project manually creates a DataTable, iterates through the collection of search results, and then populates the DataTable. In the snippet below I called the collection of search results resultElements. The snippet below shows the code that performs this step. You can download the full source code for the example project.

// Build a table based on the results of the query. dtResults.Columns.Add(new DataColumn("Title",typeof(string))); dtResults.Columns.Add(new DataColumn("Summary",typeof(string))); dtResults.Columns.Add(new DataColumn("URL",typeof(string))); // Iterate through the results collection and // build a row for each result. for (int resultCounter = 0; resultCounter < numberResults;resultCounter++) { drResult = dtResults.NewRow(); drResult["Title"] = googleSearchResult.resultElements [resultCounter].title; drResult["Summary"] = googleSearchResult.resultElements [resultCounter].snippet; drResult["URL"] = googleSearchResult.resultElements [resultCounter].URL; dtResults.Rows.Add(drResult); } // Bind the datagrid. dlResults.DataSource = dtResults; dlResults.DataBind(); pnlResults.Visible = true;

Figure 4 shows the output from the example project.

 
Figure 4. The resulting ASP.NET page with the Google Web service integrated into it.
In addition to the functionality shown above, the example project includes simplistic code that displays next and previous paging links at the bottom of the page. This lets the user navigate back and forth between the first five results and the second five results of the first ten results.

Just the Tip of the Iceberg
Hopefully this article has opened your eyes to the possibilities available for performing powerful Web searches using the Google Web service. The example project illustrates the basics of a straightforward search solution. However, the possibilities are quite broad. You could easily wrap up the entire search capabilities illustrated here into a Web User Control and use it in a small section of a page. You could perform searches using virtually any control event on an ASP.NET page including clicking linkbuttons and making a selection from a listbox.



Shannon Horn is the co-founder and chief software architect of WebGeniuses Corporation (www.webgeniuses.com). He is a Microsoft Certified Application Developer (MCAD), Microsoft Certified Solution Developer (MCSD) and a Microsoft Certified Trainer (MCT) who has been developing Microsoft Windows and Web-based solutions as well as training for over 12 years. He has been a featured speaker at many industry events including Microsoft DevDays and the ASP.NET and Web Services Solutions conference produced by asp.netPRO. He is a published author on several subjects including XML, the migration from Visual Basic 6 to VB.NET, JScript.NET, C#, and ASP.NET. Shannon speaks and trains for companies such as AppDev (www.appdev.com) and LearnIt (http://www.learnit.com) and has been a featured speaker in training videos with LearnKey. He has also worked with large corporate clients including Microsoft, Universal Studios, MGM Studios, Monster.com/FlipDog.com, Intel, Polygram Pictures, Prudential, Micro Accounting Systems, Sky Harbor International Airport, and Southern Automated Systems on projects using Microsoft technologies such as Visual FoxPro, Visual Studio.NET, C#, ADO.NET, and ASP.NET. Shannon lives in Glendale, AZ, and is married with two daughters and a son..
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date