using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Threading; namespace quoteservice { [WebService(Namespace = "http://harbormist.com/", Description = "A web service which delivers a random quote after 2 seconds. " + "[Programmer: Pat Palmer.]")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Quote : System.Web.Services.WebService { public Quote() { InitializeComponent(); } #region Component Designer generated code private IContainer components = null; private void InitializeComponent() { } protected override void Dispose( bool disposing ) { if(disposing && components != null) { components.Dispose(); } base.Dispose(disposing); } #endregion [WebMethod(Description = "Returns a quote as a string.")] public string GetQuote() { string answer = "Someday, this will be a real quote."; bool gotConnected = false; bool gotQuote = false; Thread.Sleep(2000); int qID = 0; string qBody = "Someday, this might be a real quote."; string qSource = ""; DataTable myDataTable = new DataTable(); try { System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(); sqlConnection1.ConnectionString = "workstation id=TABBY;packet size=4096;" + "user id=xxxxxxxx;" + "data source=\"sql2k502.discountasp.net\";" + "persist security info=True;" + "initial catalog=SQL2005_229378_harbor;" + "password=xxxxxxxx"; sqlConnection1.FireInfoMessageEventOnUserErrors = false; System.Data.SqlClient.SqlDataAdapter adaptSQL = new System.Data.SqlClient.SqlDataAdapter("Select * from Quotes", sqlConnection1); adaptSQL.Fill(myDataTable); // close the connection sqlConnection1.Close(); gotConnected = true; } catch (Exception ex) { ex.ToString(); } if (gotConnected) { int rowCount = 0; foreach (DataRow drow in myDataTable.Rows) rowCount++; Random r = new Random(); int myRand = r.Next(rowCount); int i = 0; foreach (DataRow drow in myDataTable.Rows) { if (i == myRand) { qID = (int)drow["QuoteID"]; qBody = (string)drow["QuoteBody"]; qSource = (string)drow["QuoteSource"]; gotQuote = true; break; } i++; } if (gotQuote) { answer = qBody + " (" + qSource + ")"; } return answer; } else // no connection { return answer; } } // end getQuote } }