using System; using System.Data; using System.Data.Common; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Collections.Generic; using System.Web.UI.HtmlControls; using System.IO; using System.Text; using System.Diagnostics; namespace SomeNamespace { public class SomeClass { private DbConnection CreateDbConnection() { // Note: CreateDbConnection() will return an opened connection object // assume this for the test } public void SomeMethod(HtmlTextWriter writer, string someValue) { try { DbConnection conn = CreateDbConnection(); DbCommand command = conn.CreateCommand(); command.CommandText = "SomeStoredProc"; command.CommandType = CommandType.StoredProcedure; command.Prepare(); DbParameter DbParm = command.CreateParameter(); DbParm.Direction = ParameterDirection.Output; DbParm.ParameterName = "@SomeValue"; DbParm.DbType = DbType.String; DbParm.Value = someValue; command.Parameters.Add(DbParm); // add return value parameter DbParameter DbParmRetVal = command.CreateParameter(); DbParmRetVal.Direction = ParameterDirection.ReturnValue; DbParmRetVal.ParameterName = "RETURN_VALUE"; DbParmRetVal.DbType = DbType.Int32; command.Parameters.Add(DbParmRetVal); DbDataReader reader = command.ExecuteReader(); if ((int)DbParmRetVal.Value == 1) { throw new Exception(); } // loop around the records while (reader.Read()) { int ID = int.Parse(reader["Id"].ToString()); string title = Convert.ToString(reader["Title"]); writer.WriteLine("

" + title + "

"); writer.AddAttribute("title", title); writer.RenderBeginTag("span"); writer.Write(title); writer.RenderEndTag(); writer.Write("Click here to link somewhere"); } writer.EndRender(); } catch { } } } }