Exemple #1
0
        /// <summary>
        /// method to update record of any student
        /// </summary>
        /// <returns>whether record updated or not</returns>
        public bool UpdateStudent()
        {
            bool result = false;

            //SqlConnection object to hold the properties of the connect
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectDB"].ToString());

            try
            {
                conn.Open();   //opening the connection to the database

                //query to be excecuted
                string query = "update student_master set FullName=@fullname, FathersName=@fathersName,RollNo=@rollNo, Age=@age, Stream=@stream, Address=@address, State=@state where StudID=@studID";

                //SqlCommand object to handle the execution of the query
                SqlCommand comm = new SqlCommand(query, conn);
                comm.Parameters.AddWithValue("fullname", this.fullName);
                comm.Parameters.AddWithValue("fathersName", this.fathersName);
                comm.Parameters.AddWithValue("rollNo", this.rollNo);
                comm.Parameters.AddWithValue("age", this.age);
                comm.Parameters.AddWithValue("stream", this.stream);
                comm.Parameters.AddWithValue("address", this.address);
                comm.Parameters.AddWithValue("state", this.state);
                comm.Parameters.AddWithValue("studID", this.StudID);
                int count = comm.ExecuteNonQuery(); //exceuting the query

                conn.Close();                       //closing the connection with database

                if (count > 0)                      //checking whether any data got updated or not ?
                {
                    result = true;
                }
            }
            catch (SqlException ex)
            {
                //if found any exception
                Console.WriteLine("{0} EX : {1} ", Messages.ConnToDBFailed, ex.ToString());
                UtilityFunctions.LogToEventLog(ex);
            }

            return(result);  //returning the result
        }
Exemple #2
0
        /// <summary>
        /// method to add a new Student record in the database
        /// </summary>
        /// <returns>whether record added or not.</returns>
        public bool AddStudent()
        {
            bool result = false;

            //SqlConnection object to hold the properties of the connect
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectDB"].ToString());

            try
            {
                //opeining the connection
                conn.Open();

                //query to be executed
                string query = "insert into student_master values(@fullname,@fathersName,@rollNo,@age,@stream,@address,@state)";

                //SqlCommand object to handle the query execution
                SqlCommand comm = new SqlCommand(query, conn);
                comm.Parameters.AddWithValue("fullname", this.fullName);
                comm.Parameters.AddWithValue("fathersName", this.fathersName);
                comm.Parameters.AddWithValue("rollNo", this.rollNo);
                comm.Parameters.AddWithValue("age", this.age);
                comm.Parameters.AddWithValue("stream", this.stream);
                comm.Parameters.AddWithValue("address", this.address);
                comm.Parameters.AddWithValue("state", this.state);
                int count = comm.ExecuteNonQuery(); //executing the query

                conn.Close();                       //closing the connection with the database

                if (count > 0)                      //checking if any record got added or not?
                {
                    result = true;
                }
            }
            catch (SqlException ex)
            {
                //if found any exception
                Console.WriteLine("{0} EX : {1} ", Messages.ConnToDBFailed, ex.ToString());
                UtilityFunctions.LogToEventLog(ex);
            }

            return(result); //returning the result
        }
Exemple #3
0
        /// <summary>
        /// Method to retreave all the records of the students
        /// </summary>
        /// <returns>list of all students</returns>
        public static List <Student> ListAllStudents()
        {
            //list to store information of the students fetched from the database
            List <Student> students = new List <Student>();

            //SqlConnection object to hold the properties of the connect
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectDB"].ToString());

            try
            {
                conn.Open();   //opening the connection with the database

                //query to be executed
                string        query  = "select * from student_master";
                SqlCommand    comm   = new SqlCommand(query, conn);
                SqlDataReader reader = comm.ExecuteReader();

                //reading the data from DataReader after fetching
                while (reader.Read())
                {
                    //creating object of Student
                    Student stud = new Student(Convert.ToInt32(reader["StudID"]), reader["FullName"].ToString(), reader["FathersName"].ToString(), Convert.ToInt32(reader["RollNo"]), Convert.ToInt32(reader["Age"]), Convert.ToInt32(reader["Stream"]), reader["Address"].ToString(), Convert.ToInt32(reader["State"]));
                    students.Add(stud); //adding the object to List
                }
                reader.Close();         //closing the reader
                conn.Close();           //closing the connection
            }
            catch (SqlException ex)
            {
                //if found any exception in opening the connection
                Console.WriteLine("{0} EX : {1} ", Messages.ConnToDBFailed, ex.ToString());
                UtilityFunctions.LogToEventLog(ex);
            }

            return(students); //returning the list
        }