Proverb Web Service: Creating the Web Service - Part 1
Add Comment<div align="center"> <table border="0" width="90%" class="outline"> <tr> <td width="40%" class="outline"><b>Download File</b></td> <td width="15%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="40%" class="outline"><a href="../../file/proverbservice1.zip" class="wbox">proverbservice1.zip</a> (12kb)</td> <td width="15%" class="outline">Beta2</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> My previous '<a href="http://www.mastercsharp.com/article.aspx?ArticleID=52&&TopicID=7" class="wbox">Horoscope Web Service</a>' example got a tremendous feedback from you all - Thanks!!<br> One request I constantly got was that even though the example was good, everyone wanted something that is updated regularly, something they can add to their web sites!!<br> Well, <b><i> Your wish My Command</i></b> :) !!</p> <p>Here I have another Web Service example for you all, the difference being that this web service gives you more interactivity!! The scenario is the same, that of content provider. This time I will serve "proverbs" to you, also you will be able to add your own proverbs to this web service (neat ... so if you want the data to be updated often, better make it a point to add some proverbs regularly!) . The current database has 18 proverbs ( <i> 18 till I die....</i>) thanks to my friend <b> Ridhish</b> for helping me with the content!</p> <p><span class=wboxheado>Proverbs Service Model</span><br> The service currently supports two methods<br> 1) <b> GetProverb</b> - This method will send a random proverb to the client<br> 2) <b> AddProverb</b> - This method is used to add a proverb to the service database for moderation.</p> <p><span class=wboxhead>Database Structure - Ms Access 2000</span><br> Table 1 - <b> Proverb</b> <table border="0" width="100%" class="outline"> <tr> <td width="50%" class="outline"><b>Column Name</b></td> <td width="50%" class="outline"><b>Data Type</b></td> </tr> <tr> <td width="50%" class="outline">ID</td> <td width="50%" class="outline">AutoNumber</td> </tr> <tr> <td width="50%" class="outline">Content</td> <td width="50%" class="outline">Memo</td> </tr> </table> <p>Table 2 - <b>Moderate</b> <table border="0" width="100%" class="outline"> <tr> <td width="50%" class="outline"><b>Columns Name</b></td> <td width="50%" class="outline"><b>Data Type</b></td> </tr> <tr> <td width="50%" class="outline">ID</td> <td width="50%" class="outline">AutoNumber</td> </tr> <tr> <td width="50%" class="outline">Content</td> <td width="50%" class="outline">Memo</td> </tr> <tr> <td width="50%" class="outline">Dt</td> <td width="50%" class="outline">String</td> </tr> </table><p><span class=wboxheado>Code</span><br> <b> ProverbService.asmx</b> - Proverb's Web Service File</p> <table border="0" width="100%" class="code"> <tr> <td width="100%"><pre><%@ WebService class="Proverbs" Language="C#" %> using System; using System.Data; using System.Data.OleDb ; using System.Web.Services; [WebService(Namespace="http://www.mastercsharp.com/WebService")] public class Proverbs: WebService { [WebMethod] public string GetProverb() { try { <span class=cmt>//Declare the connection and SQL string</span> string conString=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source="; conString+=Server.MapPath(".\\db\\proverb.mdb"); string sqlString ="SELECT * FROM Proverb"; OleDbDataAdapter proAdapter= new OleDbDataAdapter(sqlString,conString); DataSet proSet = new DataSet(); <span class=cmt>//Fill the DataSet</span> proAdapter.Fill(proSet,"Proverb"); <span class=cmt>//Create a Random Number Object</span> Random ranNum = new Random(); <span class=cmt>//Get a Random Number between 0 and the number of //records within the DataSet</span> int num = ranNum.Next(proSet.Tables[0].Rows.Count); <span class=cmt>//Return the Random proverb //Remember to add 1 to the random number since it starts from 0 //and the DataBase id starts from 1</span> return proSet.Tables[0].Rows[++num]["Content"].ToString() ; } catch(Exception ed) { return "Sorry Service Unavailable!"; } } [WebMethod] public string AddProverb(string userProverb) { if(userProverb!="") { try { <span class=cmt>//Declare the connection and SQL string</span> string conString=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source="; conString+=Server.MapPath(".\\db\\proverb.mdb"); string sqlString ="INSERT INTO moderate (Content,Dt) VALUES ('"+userProverb; sqlString+="', '"+DateTime.Now+"')" ; <span class=cmt>//Create a Command and Connection</span> OleDbConnection proConnection= new OleDbConnection(conString); OleDbCommand proCommand = new OleDbCommand(sqlString,proConnection); proConnection.Open(); <span class=cmt>//Enter the Data into the Moderate table for moderation</span> proCommand.ExecuteNonQuery(); proConnection.Close(); return "Thank You!"; } catch { return "Sorry Service Unavailable!"; } } else { return "Please Input a Proverb!"; } } }</pre></td> </tr> </table><p><span class=wboxheado>Deploy the Web Service</span><br> Once you have the above code saved to the file '<b>ProverbService.asmx</b>', create a Virtual Directory by the name '<b>ProverbService</b>'. Copy the 'ProverbService.asmx' file in this directory and the '<b>proverb.mdb</b>' database file in the '<b>db</b>' directory within the 'ProverbService' Virtual Directory.</p> <p>Once that's done, you can call the Web Service from any browser by typing the full URI to the service i.e. <i> http://localhost/ProverbService/ProverbService.asmx</i> (considering that you are deploying on a local server).</p> <p>You can view the <b> WSDL</b> of the Web Service by typing the full URI of the Web Service along with WSDL as a Query String i.e. <i>http://localhost/ProverbService/ProverbService.asmx?WSDL</i>. </p> <p><span class=wboxheado>Conclusion</span><br> In this part we saw how to create our web service, in the coming parts I will describe how to create clients and Administration interfaces for this Web Service.<br> Till then, feel free to create your own client, if you know how to consume the service.<br> Waiting for your Proverbs .....</p>