Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the Course Table:
courseNum not allow null
courseName allow null
creditHours allow null
description allow null

C#
class DataAccess
    {
        public static string connstr = 
            ConfigurationManager.ConnectionStrings["STDBCONN"].ConnectionString;
    
        public static object GetSingleAnswer(string sql)
        {
            object obj = null;
            SqlConnection conn = new SqlConnection(connstr);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                obj = cmd.ExecuteScalar();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
            return obj;
        }

        public static int InsUpDel(string sql)
        {
            int rows = 0;
            SqlConnection conn = new SqlConnection(connstr);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                rows = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
            return rows;
        }

        public static DataTable GetDataTable(string sql)
        {
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection(connstr);
            try
            {
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(dt);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }
    }


C#
class Course
    {
        string courseNum;
        public string CourseNum
        {
            get { return courseNum; }
            set { courseNum = value; }
        }
        string courseName;
        public string CourseName
        {
            get { return courseName; }
            set { courseName = value; }
        }
        int creditHours;
        public int CreditHours
        {
            get { return creditHours; }
            set { creditHours = value; }
        }
        string description;
        public string Description
        {
            get { return description; }
            set { description = value; }
        }


C#
class CourseRepository
    {
        List<Course> COList = new List<Course>();

        public List<Course> GetAllCourse()
        {
            COList.Clear();
            try
            {
                string sql = "select * from courses";
                DataTable da = DataAccess.GetDataTable(sql);
                foreach (DataRow dr in da.Rows)
                {
                    Course co = new Course();
                    co.CourseNum = (string)dr["CourseNum"];
                    co.CourseName = (string)dr["CourseName"];
                    co.CreditHours = (int)dr["CreditHours"];
                    if (!DBNull.Value.Equals(dr["Description"]))
                        co.Description = (string)dr["Description"];

                    COList.Add(co);
                }

            }
            catch (Exception)
            {
                throw;
            }
            return COList;
        }

        
        public int InsertCourse(string CourseNum, string CourseName, string CreditHours, string Description)
        {
            string sql = "select CourseNum from courses where CourseNum='" + CourseNum + "'";
            string foundCourseNum = (string)DataAccess.GetSingleAnswer(sql);

            if (CourseNum != foundCourseNum)
            {
                sql = "insert into courses (CourseNum,CourseName,CreditHours,Description) values ('" + CourseNum + "','" + CourseName + "'," + CreditHours + ",'" + Description + "')";
                return DataAccess.InsUpDel(sql);
            }
            return 0;
        }

        public int UpdateCourse(string CourseNum, string CourseName, string CreditHours, string Description)
        {
            string sql = "update courses set CourseName='" + CourseName + "',CreditHours=" + CreditHours + ",Description='" + Description + "' where CourseNum='" + CourseNum + "'";
            return DataAccess.InsUpDel(sql);
        }

        public int DeleteCourse(string CourseNum)
        {
            string sql = "delete from courses where CourseNum='" + CourseNum + "'";
            return DataAccess.InsUpDel(sql);
        }


    }


C#
class CourseBusiness
   {
       CourseRepository _crep = new CourseRepository();

       public List<Course> GetAllCourse()
       {
           return _crep.GetAllCourse();
       }

       public int InsertCourse(string CourseNum, string CourseName, string CreditHours, string Description)
       {
               return _crep.InsertCourse(CourseNum, CourseName, CreditHours, Description);
       }

       public int UpdateCourse(string CourseNum, string CourseName, string CreditHours, string Description)
       {
           return _crep.UpdateCourse(CourseNum, CourseName, CreditHours, Description);
       }

       public int DeleteCourse(string CourseNum)
       {
           return _crep.DeleteCourse(CourseNum);
       }
   }


In the form---->>>

C#
private void btnAdd_Click(object sender, EventArgs e)
{
    int rows = cou.InsertCourse((txtCourseNum.Text).ToUpper(), txtCourseName.Text, txtCreditHours.Text, txtDescription.Text);

    if(rows > 0)
    {
        txtCourseNum.Text = "";
        txtCourseName.Text = "";
        txtDescription.Text = "";
        txtCreditHours.Text = "";

        MessageBox.Show("Added");

        btnUpdate.Enabled = false;
        btnDelete.Enabled = false;

        dgv.DataSource = null;
        dgv.DataSource = cou.GetAllCourse();
        dgv.Refresh();
    }
    else if (rows == 0)
        MessageBox.Show("This Course Number already exists ..");

}
Posted
Comments
PIEBALDconsult 27-Nov-15 22:09pm    
Unclear. Without more context all I can say is, "check it in the UI".
Krunal Rohit 27-Nov-15 23:07pm    
Perform the Client-Side validation.

-KR

1 solution

The best way is to do all these validation in the UI using Javascript or ASP.Net validation controls like Required Expression Validation control.
Check following links-
ASP.NET - Validators[^]
Client Side Validation using JavaScript[^]

You can double check these in the Property layer too like following -

C#
string courseNum;
public string CourseNum
{
    get { return courseNum; }
    set {
          if(value==null) throw new ArgumentException("Course Num can't be null.");
          courseNum = value;
        }
}


Hope it helps :)
 
Share this answer
 
Comments
Member 11232188 28-Nov-15 8:50am    
where is best place to put the validation in course class or in the business layer? if we talk about the performance and validation in server side
Suvendu Shekhar Giri 28-Nov-15 9:15am    
My recommendation is both, in the UI and Business Layer/Property layer(if it's separate layer in your application).
Sometimes if javascript is blocked in the browser then you can make sure that data goes in to your application are validated.
Member 11232188 28-Nov-15 9:12am    
If I applied to put the validation in course class, how can I handle the exception in the UI?
Suvendu Shekhar Giri 28-Nov-15 9:19am    
Validation in the UI is a must because for validation kind of stuffs you don't want users wait till a postback happens. It's always better to have client side validation as user can see the msgs immediately.
Member 11232188 28-Nov-15 9:23am    
I mean how I can get the message "Course Num can't be null." in the form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900