using System; using System.Data; using System.Data.SqlClient; using System.Collections.Specialized; using System.Text; public partial class DatabaseClass { #region Contact Form public DataTable ContactForm_GetAll() { SqlCommand cmd = new SqlCommand("SELECT *, (SELECT COUNT(*) FROM ContactFormSubmissions WHERE ContactFormID=ContactForms.ContactFormID) as NumSubmissions FROM ContactForms ORDER BY BlockTitle", Connection); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataRow ContactForm_Get(string BlockTitle) { SqlCommand cmd = new SqlCommand("SELECT * FROM ContactForms WHERE BlockTitle=@BlockTitle", Connection); cmd.Parameters.AddWithValue("@BlockTitle", BlockTitle); DataRow dr = GetDataRow(cmd); cmd.Dispose(); return dr; } public DataRow ContactForm_GetByContactFormID(int ContactFormID) { SqlCommand cmd = new SqlCommand("SELECT * FROM ContactForms WHERE ContactFormID=@ContactFormID", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); DataRow dr = GetDataRow(cmd); cmd.Dispose(); return dr; } public void ContactForm_AutoGenerate(string BlockTitle) { SqlCommand cmd = new SqlCommand("INSERT INTO ContactForms (BlockTitle,Subject,FromName,FromAddress,RecipientAddresses,ThankYouMessage) VALUES (@BlockTitle,@Subject,@FromName,@FromAddress,@RecipientAddresses,@ThankYouMessage)", Connection); cmd.Parameters.AddWithValue("@BlockTitle", BlockTitle); cmd.Parameters.AddWithValue("@Subject", BlockTitle); cmd.Parameters.AddWithValue("@FromName", "Website"); cmd.Parameters.AddWithValue("@FromAddress", "noreply@webadvanced.com"); cmd.Parameters.AddWithValue("@RecipientAddresses", "mjdunn@webadvanced.com"); cmd.Parameters.AddWithValue("@ThankYouMessage", "Your submission has been received!"); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void ContactForm_Update(int ContactFormID, string Subject, string FromName, string FromAddress, string RecipientAddresses, string ThankYouMessage) { SqlCommand cmd = new SqlCommand("UPDATE ContactForms SET Subject=@Subject, FromName=@FromName, FromAddress=@FromAddress, RecipientAddresses=@RecipientAddresses, ThankYouMessage=@ThankYouMessage WHERE ContactFormID=@ContactFormID", Connection); cmd.Parameters.AddWithValue("@Subject", Subject); cmd.Parameters.AddWithValue("@FromName", FromName); cmd.Parameters.AddWithValue("@FromAddress", FromAddress); cmd.Parameters.AddWithValue("@RecipientAddresses", RecipientAddresses); cmd.Parameters.AddWithValue("@ThankYouMessage", ThankYouMessage); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); cmd.ExecuteNonQuery(); cmd.Dispose(); } #endregion #region Contact Form Fields public DataRow ContactFormFields_Get(int ContactFormFieldID) { SqlCommand cmd = new SqlCommand("SELECT * FROM ContactFormFields WHERE ContactFormFieldID=@ContactFormFieldID", Connection); cmd.Parameters.AddWithValue("@ContactFormFieldID", ContactFormFieldID); DataRow dr = GetDataRow(cmd); cmd.Dispose(); return dr; } public DataTable ContactFormFields_GetByContactFormID(int ContactFormID) { SqlCommand cmd = new SqlCommand("SELECT * FROM ContactFormFields WHERE ContactFormID=@ContactFormID ORDER BY SortOrder", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public void ContactFormFields_Update(int ContactFormFieldID, int ContactFormID, int FieldType, string Label, string Options, string Validation, bool Required) { SqlCommand cmd = new SqlCommand("UPDATE ContactFormFields SET FieldType=@FieldType, Label=@Label, Options=@Options, Validation=@Validation, Required=@Required WHERE ContactFormFieldID=@ContactFormFieldID", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); cmd.Parameters.AddWithValue("@FieldType", FieldType); cmd.Parameters.AddWithValue("@Label", Label); cmd.Parameters.AddWithValue("@Options", Options); cmd.Parameters.AddWithValue("@Validation", Validation); cmd.Parameters.AddWithValue("@Required", Required); cmd.Parameters.AddWithValue("@ContactFormFieldID", ContactFormFieldID); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void ContactFormFields_Add(int ContactFormID, int FieldType, string Label, string Options, string Validation, bool Required) { SqlCommand cmd = new SqlCommand("INSERT INTO ContactFormFields (ContactFormID,FieldType,Label,Options,Validation,Required) VALUES (@ContactFormID,@FieldType,@Label,@Options,@Validation,@Required)", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); cmd.Parameters.AddWithValue("@FieldType", FieldType); cmd.Parameters.AddWithValue("@Label", Label); cmd.Parameters.AddWithValue("@Options", Options); cmd.Parameters.AddWithValue("@Validation", Validation); cmd.Parameters.AddWithValue("@Required", Required); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void ContactFormFields_Delete(int ContactFormFieldID) { SqlCommand cmd = new SqlCommand("DELETE FROM ContactFormFields WHERE ContactFormFieldID=@ContactFormFieldID", Connection); cmd.Parameters.AddWithValue("@ContactFormFieldID", ContactFormFieldID); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void ContactFormFields_UpdateSort(System.Collections.Specialized.NameValueCollection nvColl) { SqlCommand cmd = new SqlCommand("UPDATE ContactFormFields SET SortOrder=@SortOrder WHERE ContactFormFieldID=@ContactFormFieldID", Connection); cmd.Parameters.Add("@SortOrder", SqlDbType.Int); cmd.Parameters.Add("@ContactFormFieldID", SqlDbType.Int); foreach (string id in nvColl.Keys) { cmd.Parameters["@ContactFormFieldID"].Value = id; cmd.Parameters["@SortOrder"].Value = nvColl[id]; cmd.ExecuteNonQuery(); } cmd.Dispose(); } #endregion #region Contact Form Submissions public void ContactFormSubmissions_Add(int ContactFormID, NameValueCollection nvColl, string ErrorMessage) { SqlCommand cmd = new SqlCommand("INSERT INTO ContactFormSubmissions (ContactFormID, ErrorMessage, TimeStamp) VALUES (@ContactFormID, @ErrorMessage, getDate()); SELECT @@IDENTITY;", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); cmd.Parameters.AddWithValue("@ErrorMessage", (ErrorMessage == null ? (object)DBNull.Value : (object)ErrorMessage)); int ContactFormSubmissionID = (int)(decimal)cmd.ExecuteScalar(); cmd.Dispose(); cmd = new SqlCommand("INSERT INTO ContactFormSubmissionData (ContactFormSubmissionID, Label, Value) VALUES (@ContactFormSubmissionID, @Label, @Value)", Connection); cmd.Parameters.AddWithValue("@ContactFormSubmissionID", ContactFormSubmissionID); cmd.Parameters.Add("@Label", SqlDbType.VarChar); cmd.Parameters.Add("@Value", SqlDbType.VarChar); foreach (string label in nvColl.Keys) { cmd.Parameters["@Label"].Value = label; cmd.Parameters["@Value"].Value = nvColl[label]; cmd.ExecuteNonQuery(); } cmd.Dispose(); } public DataTable ContactFormSubmissions_GetPaged(int ContactFormID, int PageIndex, int PageSize) { int LowResult = ((PageIndex - 1) * PageSize) + 1; int HighResult = LowResult + PageSize - 1; SqlCommand cmd = new SqlCommand(@" WITH OrderedResults AS ( SELECT ContactFormSubmissionID, TimeStamp, ROW_NUMBER() OVER (ORDER BY Timestamp DESC) as 'RowNumber' FROM ContactFormSubmissions WHERE ContactFormID=@ContactFormID) SELECT * FROM OrderedResults WHERE RowNumber BETWEEN @LowResult AND @HighResult;", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); cmd.Parameters.AddWithValue("@LowResult", LowResult); cmd.Parameters.AddWithValue("@HighResult", HighResult); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataTable ContactFormSubmissions_ExportPage(int ContactFormID, int PageIndex, int PageSize) { int LowResult = ((PageIndex - 1) * PageSize) + 1; int HighResult = LowResult + PageSize - 1; SqlCommand cmd = new SqlCommand(@"WITH OrderedResults AS ( SELECT ContactFormSubmissionID, ROW_NUMBER() OVER (ORDER BY Timestamp DESC) as 'RowNumber' FROM ContactFormSubmissions WHERE ContactFormID=@ContactFormID) SELECT submission.Timestamp, data.Label, data.Value FROM ContactFormSubmissionData data INNER JOIN ContactFormSubmissions submission ON data.ContactFormSubmissionID=submission.ContactFormSubmissionID WHERE data.ContactFormSubmissionID IN (SELECT ContactFormSubmissionID FROM OrderedResults WHERE RowNumber BETWEEN @LowResult AND @HighResult) ORDER BY Timestamp DESC, Label", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); cmd.Parameters.AddWithValue("@LowResult", LowResult); cmd.Parameters.AddWithValue("@HighResult", HighResult); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataTable ContactFormSubmissions_ExportAll(int ContactFormID) { SqlCommand cmd = new SqlCommand(@"SELECT submission.Timestamp, data.Label, data.Value FROM ContactFormSubmissionData data INNER JOIN ContactFormSubmissions submission ON data.ContactFormSubmissionID=submission.ContactFormSubmissionID WHERE data.ContactFormSubmissionID IN (SELECT ContactFormSubmissionID FROM ContactFormSubmissions WHERE ContactFormID=@ContactFormID) ORDER BY Label, Timestamp DESC", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public int ContactFormSubmissions_GetCount(int ContactFormID) { SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM ContactFormSubmissions WHERE ContactFormID=@ContactFormID", Connection); cmd.Parameters.AddWithValue("@ContactFormID", ContactFormID); int Res = (int)cmd.ExecuteScalar(); cmd.Dispose(); return Res; } public DataTable ContactFormSubmissionData_Get(int ContactFormSubmissionID) { SqlCommand cmd = new SqlCommand("SELECT * FROM ContactFormSubmissionData WHERE ContactFormSubmissionID=@ContactFormSubmissionID", Connection); cmd.Parameters.AddWithValue("@ContactFormSubmissionID", ContactFormSubmissionID); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } #endregion #region RSS public DataTable RSS_GetAll(bool ShowOnHomepage) { SqlCommand cmd = new SqlCommand("SELECT * FROM Rss WHERE ~(@ShowOnHomepage & ~ShowOnHomepage) = 1 ORDER BY RssSource", Connection); cmd.Parameters.AddWithValue("@ShowOnHomepage", ShowOnHomepage); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataTable RSS_Get(int RssID, bool ShowOnHomepage) { SqlCommand cmd = new SqlCommand("SELECT * FROM Rss WHERE ~(@ShowOnHomepage & ~ShowOnHomepage) AND RssID=@RssID", Connection); cmd.Parameters.AddWithValue("@ShowOnHomepage", ShowOnHomepage); cmd.Parameters.AddWithValue("@RssID", RssID); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } #endregion #region Text Block public DataTable TextBlock_GetAll() { SqlCommand cmd = new SqlCommand("SELECT Content.*, Drafts.DraftID AS dID FROM Content LEFT OUTER JOIN Drafts ON Content.ContentTitle = Drafts.ContentTitle ORDER BY Location, ContentTitle", Connection); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataRow TextBlock_Get(string BlockTitle) { // pull draft data SqlCommand cmd = new SqlCommand("SELECT COALESCE(d.ContentValue, c.ContentValue) as ContentText, c.*, d.DraftID FROM Content c LEFT OUTER JOIN Drafts d ON c.ContentTitle = d.ContentTitle WHERE c.ContentTitle=@ContentTitle", Connection); cmd.Parameters.AddWithValue("@ContentTitle", BlockTitle); DataRow dr = GetDataRow(cmd); cmd.Dispose(); return dr; } public DataTable TextBlock_GetUniqueLocations() { SqlCommand cmd = new SqlCommand("SELECT DISTINCT Location FROM Content WHERE Location IS NOT NULL", Connection); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public void TextBlock_AutoGenerate(string BlockTitle, string DefaultText, string Location) { SqlCommand cmd = new SqlCommand("AutoNewTextBlock", Connection); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ContentTitle", BlockTitle); cmd.Parameters.AddWithValue("@ContentValue", DefaultText); cmd.Parameters.AddWithValue("@PageLocation", Location); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void TextBlock_DeleteDraft(int DraftID) { SqlCommand cmd = new SqlCommand("DELETE FROM Drafts WHERE DraftID=@DraftID", Connection); cmd.Parameters.AddWithValue("@DraftID", DraftID); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void TextBlock_Update(string BlockTitle, string ContentValue) { SqlCommand cmd = new SqlCommand("UPDATE Content SET ContentValue=@ContentValue WHERE ContentTitle=@ContentTitle", Connection); cmd.Parameters.AddWithValue("@ContentValue", ContentValue); cmd.Parameters.AddWithValue("@ContentTitle", BlockTitle); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void TextBlock_CreateDraft(string BlockTitle, string ContentValue) { SqlCommand cmd = new SqlCommand("INSERT INTO Drafts (ContentTitle, ContentValue) VALUES (@ContentTitle, @ContentValue)", Connection); cmd.Parameters.AddWithValue("@ContentTitle", BlockTitle); cmd.Parameters.AddWithValue("@ContentValue", ContentValue); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void TextBlock_UpdateDraft(int DraftID, string BlockTitle, string ContentValue) { SqlCommand cmd = new SqlCommand("UPDATE Drafts SET ContentTitle=@ContentTitle, ContentValue=@ContentValue WHERE DraftID=@DraftID", Connection); cmd.Parameters.AddWithValue("@DraftID", DraftID); cmd.Parameters.AddWithValue("@ContentTitle", BlockTitle); cmd.Parameters.AddWithValue("@ContentValue", ContentValue); cmd.ExecuteNonQuery(); cmd.Dispose(); } #endregion #region News And Events public DataTable NewsAndEvents_Get(string Category) { SqlCommand cmd = new SqlCommand("SELECT * FROM Events WHERE Category=COALESCE(@Category, Category) ORDER BY Date DESC, EventID DESC", Connection); cmd.Parameters.AddWithValue("@Category", (Category == null ? (object)DBNull.Value : (object)Category)); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataTable NewsAndEvents_GetTop(string Category, int Top) { SqlCommand cmd = new SqlCommand("SELECT Top (@Num) * FROM Events WHERE Category=COALESCE(@Category, Category) ORDER BY Date DESC, EventID DESC", Connection); cmd.Parameters.AddWithValue("@Num", Top); cmd.Parameters.AddWithValue("@Category", (Category == null ? (object)DBNull.Value : (object)Category)); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataRow NewsAndEvents_GetByEventID(int EventID) { SqlCommand cmd = new SqlCommand("SELECT * FROM Events WHERE EventID=@EventID", Connection); cmd.Parameters.AddWithValue("@EventID", EventID); DataRow dr = GetDataRow(cmd); cmd.Dispose(); return dr; } #endregion #region Users public DataRow Users_GetByUsernameAndPassword(string Email, string Password) { SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE Email=@Email AND Password=@Password", Connection); cmd.Parameters.AddWithValue("@Email", Email); cmd.Parameters.AddWithValue("@Password", Password); DataRow dr = GetDataRow(cmd); cmd.Dispose(); return dr; } public DataTable Users_GetAll() { SqlCommand cmd = new SqlCommand("SELECT * FROM Users ORDER BY Name", Connection); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataRow Users_GetByUserID(int UserID) { SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE UserID=@UserID", Connection); cmd.Parameters.AddWithValue("@UserID", UserID); DataRow dr = GetDataRow(cmd); cmd.Dispose(); return dr; } public void Users_Add(string Name, string Email, string Password) { SqlCommand cmd = new SqlCommand("INSERT INTO Users (Name, Email, Password) VALUES (@Name, @Email, @Password)", Connection); cmd.Parameters.AddWithValue("@Name", Name); cmd.Parameters.AddWithValue("@Email", Email); cmd.Parameters.AddWithValue("@Password", Password); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void Users_Update(int UserID, string Name, string Email, string Password) { SqlCommand cmd = new SqlCommand("UPDATE Users SET Name=@Name, Email=@Email, Password=@Password WHERE UserID=@UserID", Connection); cmd.Parameters.AddWithValue("@Name", Name); cmd.Parameters.AddWithValue("@Email", Email); cmd.Parameters.AddWithValue("@Password", Password); cmd.Parameters.AddWithValue("@UserID", UserID); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void Users_Delete(int UserID) { SqlCommand cmd = new SqlCommand("DELETE FROM Users WHERE UserID=@UserID", Connection); cmd.Parameters.AddWithValue("@UserID", UserID); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void Users_SetActive(int UserID, bool Active) { SqlCommand cmd = new SqlCommand("UPDATE Users SET Active=@Active WHERE UserID=@UserID", Connection); cmd.Parameters.AddWithValue("@Active", Active); cmd.Parameters.AddWithValue("@UserID", UserID); cmd.ExecuteNonQuery(); cmd.Dispose(); } #endregion #region Form Submissions public DataTable FormSubmissions_GetFieldsByFormName(string FormName) { SqlCommand cmd = new SqlCommand("SELECT DISTINCT FieldName FROM FormSubmissions fs INNER JOIN FormData fd ON fs.FormSubmissionID = fd.FormSubmissionID WHERE FormName=@FormName", Connection); cmd.Parameters.AddWithValue("@FormName", FormName); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataTable FormSubmissions_GetDataByFormName(string formName) { SqlCommand cmd = new SqlCommand("SELECT * FROM FormSubmissions fs INNER JOIN FormData fd ON fs.FormSubmissionID = fd.FormSubmissionID WHERE FormName=@FormName ORDER BY fs.FormSubmissionID", Connection); cmd.Parameters.AddWithValue("@FormName", formName); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataTable FormSubmissions_GetAll() { SqlCommand cmd = new SqlCommand("SELECT * FROM FormSubmissions ORDER BY TimeStamp DESC", Connection); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataTable FormSubmissions_GetUniqueFormNames() { SqlCommand cmd = new SqlCommand("SELECT DISTINCT FormName FROM FormSubmissions", Connection); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataTable FormSubmissions_GetDataByFormSubmissionID(int FormSubmissionID) { SqlCommand cmd = new SqlCommand("SELECT * FROM FormData WHERE FormSubmissionID=@FormSubmissionID", Connection); cmd.Parameters.AddWithValue("@FormSubmissionID", FormSubmissionID); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } #endregion #region Events public DataTable Events_GetAll() { SqlCommand cmd = new SqlCommand("SELECT * FROM Events ORDER BY Date desc,EventID", Connection); DataTable dt = GetDataTable(cmd); cmd.Dispose(); return dt; } public DataRow Events_GetByEventID(int EventID) { SqlCommand cmd = new SqlCommand("SELECT * FROM Events WHERE EventID=@EventID", Connection); cmd.Parameters.AddWithValue("@EventID", EventID); DataRow dr = GetDataRow(cmd); cmd.Dispose(); return dr; } public void Events_Delete(int EventID) { SqlCommand cmd = new SqlCommand("DELETE FROM Events WHERE EventID=@EventID", Connection); cmd.Parameters.AddWithValue("@EventID", EventID); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void Events_Add(string Title, DateTime? selectedDate, string Category, string FullText) { SqlCommand cmd = new SqlCommand("INSERT INTO Events (Title,Date,Category,FullText) VALUES (@title,@date,@category,@fulltext)", Connection); cmd.Parameters.AddWithValue("@title", Title); cmd.Parameters.AddWithValue("@date", (selectedDate == null ? (object)DBNull.Value : (object)selectedDate)); cmd.Parameters.AddWithValue("@category", Category); cmd.Parameters.AddWithValue("@fulltext", FullText); cmd.ExecuteNonQuery(); cmd.Dispose(); } public void Events_Update(int EventID, string Title, DateTime? selectedDate, string Category, string FullText) { SqlCommand cmd = new SqlCommand("UPDATE Events SET Title=@title,Date=@date,Category=@category,FullText=@fulltext WHERE EventID=@EventID", Connection); cmd.Parameters.AddWithValue("@title", Title); cmd.Parameters.AddWithValue("@date", (selectedDate == null ? (object)DBNull.Value : (object)selectedDate)); cmd.Parameters.AddWithValue("@category", Category); cmd.Parameters.AddWithValue("@fulltext", FullText); cmd.Parameters.AddWithValue("@EventID", EventID); cmd.ExecuteNonQuery(); cmd.Dispose(); } #endregion }