using System; using System.Collections.Generic; using System.Linq; using System.Web; using CodeCafe.Web; using System.Data.SqlClient; using System.Web.UI.WebControls; /// /// Class to handle all database transactions to the News table. /// Generated by Code Cafe Class Generator Version 1.0 /// Create date: 12/3/2010 4:55:34 PM /// public class clsNews { /****************************************************************************************************/ MSSqlTools db = new MSSqlTools(); SqlDataReader rdr = null; SqlCommand cmd; public int ni_id; public string ni_title; public string ni_author; public DateTime ni_date; public string ni_snippet; public string ni_copy; /****************************************************************************************************/ public clsNews() { Clear(); } /****************************************************************************************************/ /// /// Clears all the public variables /// public void Clear() { ni_id = 0; ni_title = ""; ni_author = ""; ni_date = DateTime.Now; ni_snippet = ""; ni_copy = ""; } /***************************************************************************************************************************************/ /// /// Populates a drop down list with values from the Database /// /// /// The drop down list to populate all items in the list will be cleared /// /// /// The Select record ID of a list item /// /// /// Specifies the type of list /// public void List(DropDownList List, int SelID, ListTypes ListType) { List.Items.Clear(); switch (ListType) { case ListTypes.AllowNew: List.Items.Add(new ListItem("- Add New -", "0")); break; case ListTypes.AllowSelect: List.Items.Add(new ListItem("- Select -", "0")); break; case ListTypes.AllowNone: List.Items.Add(new ListItem("- None -", "0")); break; } try { db.DBase.Open(); cmd = new SqlCommand("News_List", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; SqlParameter Error = cmd.Parameters.AddWithValue("@ErrorCode", 0); Error.Direction = System.Data.ParameterDirection.Output; rdr = cmd.ExecuteReader(); while (rdr.Read()) { ListItem item = new ListItem(rdr["ni_title"].ToString(), rdr["ni_id"].ToString()); item.Selected = (int.Parse(rdr["ni_id"].ToString()) == SelID); List.Items.Add(item); } rdr.Close(); } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } } /****************************************************************************************************/ /// /// Loads record specified by ID and populates values into the public variables /// /// /// Specifies the Primary key value of the record to load /// /// /// True on successful load /// False if an error occurs /// public bool Load(int ContentID) { bool result = false; try { db.DBase.Open(); cmd = new SqlCommand("News_LoadByID", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ID", ContentID); SqlParameter Error = cmd.Parameters.AddWithValue("@ErrorCode", 0); Error.Direction = System.Data.ParameterDirection.Output; rdr = cmd.ExecuteReader(); if (rdr.Read()) { ni_id = db.ProcessField(rdr["ni_id"], 0); ni_title = db.ProcessField(rdr["ni_title"], ""); ni_author = db.ProcessField(rdr["ni_author"], ""); ni_date = db.ProcessField(rdr["ni_date"], DateTime.Now); ni_snippet = db.ProcessField(rdr["ni_snippet"], ""); ni_copy = db.ProcessField(rdr["ni_copy"], ""); } rdr.Close(); result = true; } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } return result; } /****************************************************************************************************/ /// /// Loads the latest article. /// /// public bool LoadLatest() { bool result = false; try { db.DBase.Open(); cmd = new SqlCommand("News_ListLatest", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; rdr = cmd.ExecuteReader(); if (rdr.Read()) { ni_id = db.ProcessField(rdr["ni_id"], 0); ni_title = db.ProcessField(rdr["ni_title"], ""); ni_author = db.ProcessField(rdr["ni_author"], ""); ni_date = db.ProcessField(rdr["ni_date"], DateTime.Now); ni_snippet = db.ProcessField(rdr["ni_snippet"], ""); ni_copy = db.ProcessField(rdr["ni_copy"], ""); } rdr.Close(); result = true; } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } return result; } /****************************************************************************************************/ /// /// Saves values contained in the public variables to a new record if Primary Key ID is 0 or updates /// an existing record if Primary Key ID > 0 /// /// /// The ID of record saved or updated. /// public int Save() { int result = 0; try { db.DBase.Open(); cmd = new SqlCommand("News_Save", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ni_id", ni_id); cmd.Parameters.AddWithValue("@ni_title", ni_title); cmd.Parameters.AddWithValue("@ni_author", ni_author); cmd.Parameters.AddWithValue("@ni_date", ni_date); cmd.Parameters.AddWithValue("@ni_snippet", ni_snippet); cmd.Parameters.AddWithValue("@ni_copy", ni_copy); SqlParameter Saved = cmd.Parameters.AddWithValue("@SavedID", 0); Saved.Direction = System.Data.ParameterDirection.Output; cmd.ExecuteNonQuery(); result = (int)cmd.Parameters["@SavedID"].Value; } catch (Exception err) { Logger.Log(err); result = 0; } finally { db.DBase.Close(); } return result; } /****************************************************************************************************/ /// /// Deletes the record specified by ID /// /// /// Specifies the Primary key value of the record to delete /// /// /// True on successful delete /// False if an error occurs /// public bool Delete(int ContentID) { bool result = false; try { db.DBase.Open(); cmd = new SqlCommand("News_Delete", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ID", ContentID); SqlParameter Error = cmd.Parameters.AddWithValue("@ErrorCode", 0); Error.Direction = System.Data.ParameterDirection.Output; cmd.ExecuteNonQuery(); result = true; } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } return result; } /****************************************************************************************************/ /// /// Lists all news articles for sitemap. /// /// public string ListForSitemap() { string result = ""; try { db.DBase.Open(); cmd = new SqlCommand("News_ListAll", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; rdr = cmd.ExecuteReader(); while (rdr.Read()) { result += "" + rdr["ni_title"].ToString() + "\n"; } rdr.Close(); } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } return result; } /****************************************************************************************************/ }