Ejemplo n.º 1
0
        //Method to delete data from database
        public bool Delete(ContactClass cc)
        {
            //Creating a default return type and initializing it to false
            bool IsSuccess = false;

            //Step 1: Database Connection
            SqlConnection Conn = new SqlConnection(MyConnString);

            try
            {
                //Writing sql query
                string Sql = "DELETE FROM Contacts WHERE Contact_ID=@ContactID";

                //Creating cmd using Sql and Conn
                SqlCommand SqlCmd = new SqlCommand(Sql, Conn);
                SqlCmd.Parameters.AddWithValue("@ContactID", cc.ContactID);

                //Opening Connection
                Conn.Open();

                int Rows = SqlCmd.ExecuteNonQuery();
                //if query runs successfully value of rows will be greater than 0 else less than 0
                if (Rows > 0)
                {
                    IsSuccess = true;
                }
                else
                {
                    IsSuccess = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //Closing connection
                Conn.Close();
            }
            return(IsSuccess);
        }
Ejemplo n.º 2
0
        //Method to update data in Database
        public bool Update(ContactClass cc)
        {
            //Creating a default return type and initializing it to false
            bool IsSuccess = false;

            //Step 1: Database Connection
            SqlConnection Conn = new SqlConnection(MyConnString);

            try
            {
                //Writing sql query
                string Sql = "UPDATE Contacts SET First_Name=@FirstName, Last_Name=@LastName, Email_Id=@EmailId, Contact_No=@ContactNo, Address=@Address, Gender=@Gender WHERE Contact_ID=@ContactID";

                //Creating cmd using Sql and Conn
                SqlCommand SqlCmd = new SqlCommand(Sql, Conn);

                //Create Parameters to add data
                SqlCmd.Parameters.AddWithValue("@FirstName", cc.FirstName);
                SqlCmd.Parameters.AddWithValue("@LastName", cc.LastName);
                SqlCmd.Parameters.AddWithValue("@EmailId", cc.EmailId);
                SqlCmd.Parameters.AddWithValue("@ContactNo", cc.ContactNo);
                SqlCmd.Parameters.AddWithValue("@Address", cc.Address);
                SqlCmd.Parameters.AddWithValue("@Gender", cc.Gender);
                SqlCmd.Parameters.AddWithValue("@ContactID", cc.ContactID);

                //Opening Connection
                Conn.Open();

                if (cc.FirstName == "" || cc.LastName == "" || cc.EmailId == "" || cc.ContactNo == "" || cc.Address == "" || cc.Gender == "")
                {
                    MessageBox.Show("Please fill all the fields to add new contact", "Empty Fields Detected", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if (!ValidateEmail())
                {
                    MessageBox.Show("Enter a valid email address", "Invalid email address", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    int Rows = SqlCmd.ExecuteNonQuery();
                    //if query runs successfully value of rows will be greater than 0 else less than 0
                    if (Rows > 0)
                    {
                        IsSuccess = true;
                    }
                    else
                    {
                        IsSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //Closing connection
                Conn.Close();
            }
            return(IsSuccess);
        }