Beispiel #1
0
        public bool Update(ContactClass c)
        {
            bool          isSuccess = false;
            SqlConnection conn      = new SqlConnection(myconnstring);

            try
            {
                // sql to Upadte the database
                string sql = "UPDATE tbl_Contact SET FirstName=@FirstName, LastName=@LastName, TelNo=@TelNo, MobileNo=@MobileNo, Address=@Address, Gender=@Gender WHERE ContactID=@ContactID";
                // creating cmd using sql and conn
                SqlCommand cmd = new SqlCommand(sql, conn);
                // Create Parameters to add data
                cmd.Parameters.AddWithValue("FirstName", c.FirstName);
                cmd.Parameters.AddWithValue("LastName", c.LastName);
                cmd.Parameters.AddWithValue("TelNo", c.TelNo);
                cmd.Parameters.AddWithValue("MobileNo", c.MobileNo);
                cmd.Parameters.AddWithValue("Address", c.Address);
                cmd.Parameters.AddWithValue("Gender", c.Gender);
                cmd.Parameters.AddWithValue("ContactID", c.ContactID);
                // Open Database
                conn.Open();
                int rows = cmd.ExecuteNonQuery();
                // If query successfull rows value > 0. Failed rows value = 0
                if (rows > 0)
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ex " + ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return(isSuccess);
        }
Beispiel #2
0
        public bool DELETE(ContactClass c)
        {
            bool          isSuccess = false;
            SqlConnection conn      = new SqlConnection(myconnstring);

            try
            {
                // sql to Delete data in the database
                string sql = "DELETE FROM tbl_Contact WHERE ContactID=@ContactID";
                // creating cmd using sql and conn
                SqlCommand cmd = new SqlCommand(sql, conn);
                // Create Parameters to DELETE data
                cmd.Parameters.AddWithValue("ContactID", c.ContactID);
                // Open Database
                conn.Open();
                int rows = cmd.ExecuteNonQuery();
                // If query successfull rows value > 0. Failed rows value = 0
                if (rows > 0)
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ex " + ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return(isSuccess);
        }