Event Management class example : A very simple and straight forward class to performs various operation of event management using C#.
Create Class_SQLHelper.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Class_SQLHelper
/// </summary>
public class Class_SQLHelper
{
private SqlCommand cmd = new SqlCommand();
private SqlConnection conn;
private bool bool_Status;
private string str_Error;
public Class_SQLHelper()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ToString());
}
protected void OpenConnection()
{
try
{
if (conn.State != ConnectionState.Open)
conn.Open();
}
catch (Exception ex)
{
OperationStatus = false;
ErrorMessage = "Error : " + ex.Message;
}
}
protected void CloseConnection()
{
try
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
catch (Exception ex)
{
OperationStatus = false;
ErrorMessage = "Error : " + ex.Message;
}
}
public SqlConnection Connection
{
get { return conn; }
}
public SqlCommand Command
{
get { return cmd; }
set { cmd = value; }
}
public bool OperationStatus
{
get { return bool_Status; }
set { bool_Status = value; }
}
public string ErrorMessage
{
get { return str_Error; }
set { str_Error = value; }
}
}
Create Class_Event.cs and inherit this class with Class_SQLHelper.cs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Class_Event
/// </summary>
public class Class_Event : Class_SQLHelper
{
public int EventId { get; set; }
public int EventImageId { get; set; }
public string Event { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public DateTime Date { get; set; }
public string Organization { get; set; }
public string Description { get; set; }
public Boolean Status { get; set; }
public string EventImagePath { get; set; }
public string SearchText { get; set; }
public Boolean IsCoverImage { get; set; }
public DataTable EventTable { get; set; }
public Class_Event()
{
//
// TODO: Add constructor logic here
//
}
public void InsertEvent()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_InsertEvent";
cmd.Parameters.AddWithValue("@Event", Event);
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@Address", Address);
cmd.Parameters.AddWithValue("@Date", Date);
cmd.Parameters.AddWithValue("@Organization", Organization);
cmd.Parameters.AddWithValue("@Description", Description);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
EventId = Convert.ToInt32(cmd.ExecuteScalar());
if (EventId > 0)
{
OperationStatus = true;
}
else
{
OperationStatus = false;
ErrorMessage = "Error : Some error found please try again.";
}
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void InsertEventImages()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_InsertEventImages";
cmd.Parameters.AddWithValue("@EventID", EventId);
cmd.Parameters.AddWithValue("@EventImagePath", EventImagePath);
cmd.Parameters.AddWithValue("@IsCoverImage", IsCoverImage);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
EventId = Convert.ToInt32(cmd.ExecuteScalar());
if (EventId < 1)
{
OperationStatus = false;
ErrorMessage = "Error : Some error found please try again.";
}
else
{
OperationStatus = true;
}
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetEvent()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetEvent";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetAllEvents()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetAllEvents";
cmd.Parameters.AddWithValue("@SearchText", SearchText);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetAllEventFront()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetAllEventFront";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetTop1EventFront()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetTop1EventFront";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetEventFrontById()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetEventFrontById";
cmd.Parameters.AddWithValue("@EventId", EventId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void DeleteEventByID()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "DeleteEventByID";
cmd.Parameters.AddWithValue("@EventId", EventId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
EventId = Convert.ToInt32(cmd.ExecuteScalar());
if (EventId == -1)
{
OperationStatus = false;
ErrorMessage = "Error : Event does not Exist.";
}
else
{
OperationStatus = true;
}
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void UpdateEventStatus()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_UpdateEventStatusByID";
cmd.Parameters.AddWithValue("@EventId", EventId);
cmd.Parameters.AddWithValue("@Status", Status);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetEventByID()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetEventByID";
cmd.Parameters.AddWithValue("@EventId", EventId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void UpdateEventByID()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_UpdateEventById";
cmd.Parameters.AddWithValue("@EventId", EventId);
cmd.Parameters.AddWithValue("@Event", Event);
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@Address", Address);
cmd.Parameters.AddWithValue("@Date", Date);
cmd.Parameters.AddWithValue("@Organization", Organization);
cmd.Parameters.AddWithValue("@Description", Description);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
EventId = Convert.ToInt32(cmd.ExecuteScalar());
if (EventId == -1)
{
OperationStatus = false;
ErrorMessage = "Error : Some error found please try again.";
}
else
{
OperationStatus = true;
}
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetEventImageByID()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetEventImageByID";
cmd.Parameters.AddWithValue("@EventId", EventId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetAllEventImageByEventID()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetAllEventImageByID";
cmd.Parameters.AddWithValue("@EventId", EventId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void Insert_IsCoverEventImg()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_Insert_IsCoverEventImg";
cmd.Parameters.AddWithValue("@EventImageID", EventImageId);
cmd.Parameters.AddWithValue("@IsCoverImage", IsCoverImage);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
EventImageId = Convert.ToInt32(cmd.ExecuteScalar());
if (EventImageId == -1)
{
OperationStatus = false;
ErrorMessage = "Event Image does not Exist.";
}
else
{
OperationStatus = true;
}
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void UpdateEventImageStatusByID()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_UpdateEventImageStatusByID";
cmd.Parameters.AddWithValue("@EventImageID", EventImageId);
cmd.Parameters.AddWithValue("@Status", Status);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetEventImageByImageID()
{
try
{
SqlDataAdapter ad = new SqlDataAdapter();
DataTable Sqldatatable = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetEventByImageID";
cmd.Parameters.AddWithValue("@EventImageID", EventImageId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
ad.SelectCommand = cmd;
OpenConnection();
ad.Fill(Sqldatatable);
EventTable = Sqldatatable;
OperationStatus = true;
CloseConnection();
Sqldatatable.Dispose();
cmd.Dispose();
ad.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void UpdateEventImageByImageID()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_UpdateEventImageByImageID";
cmd.Parameters.AddWithValue("@EventID", EventId);
cmd.Parameters.AddWithValue("@EventImageID", EventImageId);
cmd.Parameters.AddWithValue("@EventImagePath", EventImagePath);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
EventImageId = Convert.ToInt32(cmd.ExecuteScalar());
if (EventImageId == -1)
{
OperationStatus = false;
ErrorMessage = "Error : Image does not exist.";
}
else
{
OperationStatus = true;
}
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void DeleteEventImageByImageID()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_DeleteEventImageByID";
cmd.Parameters.AddWithValue("@EventImageID", EventImageId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
EventImageId = Convert.ToInt32(cmd.ExecuteScalar());
if (EventImageId == -1)
{
OperationStatus = false;
ErrorMessage = "Error : Image does not Exist.";
}
else
{
OperationStatus = true;
}
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
public void GetMaxEventImageID()
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "USP_GetMaxEventImageId";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = Connection;
OpenConnection();
EventImageId = Convert.ToInt32(cmd.ExecuteScalar());
OperationStatus = true;
CloseConnection();
cmd.Dispose();
}
catch (Exception ex)
{
ErrorMessage = "Error : " + ex.Message;
OperationStatus = false;
}
finally
{
CloseConnection();
}
}
}